HW6

Analyzing different floating-point formats

Solutions to this and the next problem are due Wednesday, October 21 at 11:59pm.

This is not a partnership assignment. Hand in your own work.

Homework, part 1

Consider two representations for six-bit positive floating-point values. One—we'll call it representation B (for binary)—is a radix-2 representation. It stores an exponent in two bits, represented in two's complement. It stores the normalized significand in four bits, using the hidden bit as in the IEEE floating-point representation. Thus the value 2.5 (decimal) = 10.1 (binary) would be represented as

	01 (1) 0100

and the value 7/8 (decimal) = 0.111 (binary) would be represented as

	11 (1) 1100

The second representation—we'll call it representation Q (for quaternary)—is a radix-4 representation. Like representation B, it stores an exponent—of 4, not 2—in two bits, represented in two's complement. It stores the significand in four bits, two base-4 digits, with the first base-4 digit being to the left of the quaternary point and the second base-4 digit being to the right of the quaternary point. The significand is not necessarily normalized. 2.5 (decimal) thus is represented as

	00 1010

since 2.5 (decimal) = 2.2 (quaternary) = 40 × (2 × 40 + 2 × 4-1).

7/8 (decimal) = 3 × 4-1 + 2 × 4-2 = 4-1 × (3 × 40 + 2 × 4-1) , so it's represented as

	11 1110

The decimal fraction 3/4, which in quaternary is 0.3, has two representations, since it's expressible either as 40 × 3/4 or 4-1 × 3:

	11 1100
	00 0011

Part a

Find a value representable in representation B and not in representation Q. Defend your answer.

Part b

Fill out the following table.

maximum representable value (in decimal) minimum positive representable value (in decimal) number of distinct representable values other than 0
representation B
representation Q

Submission instructions

Create a directory called hw6 and place your solution to this problem in a file hw6-1 for inclusion in submit.

Homework, part 2 - Using floating point

This homework problem will give you a little experience using floating point numbers in C and a little picture of where you need to be careful.

Part a

Write a short C program that takes an integer input from the command line and prints the (x,y) coordinates of n equally spaced points around the unit circle and verifies that x2+y2=1 for all these points. For example:

$ ./circle 4
(1.000000,0.000000)     r:1.000000
(-0.000000,1.000000)    r:1.000000
(-1.000000,-0.000000)   r:1.000000
(0.000000,-1.000000)    r:1.000000

$ ./circle 5
(1.000000,0.000000)     r:1.000000
(0.309017,0.951057)     r:1.000000
(-0.809017,0.587785)    r:1.000000
(-0.809017,-0.587785)   r:1.000000
(0.309017,-0.951056)    r:1.000000

You will need to use functions in the math.h library. Doing so requires two steps:

Add your program to the file you'll be submitting to Expertiza.

Part b

In high school you learned that (x+y)*(x–y) = x2 – y2. It would be nice if this continued to be true with the finite precision numbers that we actually compute with. Here's a little program that checks this.

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char *argv[]) {
  float x = atof(argv[1]);
  float y = atof(argv[2]);
  printf("x: %f\n",x);
  printf("y: %f\n",y);
  printf("x^2 - y^2: \t%f\n",   x*x-y*y);
  printf("(x+y)*(x-y): \t%f\n",(x+y)*(x-y));
}

Try feeding it some numbers and see if you can violate the axiom. This will be difficult because the floating point expressions are computed in extended precision. Instead, experiment with this alternative that stores the intermediate values in a single precision float variable.

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char *argv[]) {
  float x = atof(argv[1]);
  float y = atof(argv[2]);
  float xx = x*x;
  float yy = y*y;
  float xpy = x+y;
  float xmy = x-y;
  float p1 = xx - yy;
  float p2 = xpy * xmy;
  printf("x: %f\n",x);
  printf("y: %f\n",y);
  printf("x^2 - y^2: \t%f\n",p1);
  printf("(x+y)*(x-y): \t%f\n",p2);
  printf("x^2 - y^2: \t%f\n",   x*x-y*y);
  printf("(x+y)*(x-y): \t%f\n",(x+y)*(x-y));
}

Find a pair of values that violate the axiom. Which is more accurate: p1 or p2? In a sentence or two explain why. Add all this information to the file hw6-2 in hw6 included in submit

Homework, part 3

Here is some C code:

    int array1[10];
    void funct (void) {
        int array2[10];

        array2[3] = array1[3];
    }

(The procedure doesn't do anything very interesting.)

  1. Give as specific a description as possible of the code that the C compiler will generate to load register $t0 with array1[3]. (Actual instructions are more specific than pseudocode.)

  2. Give as specific a description as possible of the code that the C compiler will generate to store register $t0 into array2[3].

  3. For each of the code segments in parts a and b, we want to know the earliest stage in the compilation process at which the final numeric values of the fields in all the relevant instructions are known.

    Are the numeric fields in the instructions that load $t0 with array1[3] first known ...

    Briefly explain.

  4. Are the numeric fields in the instructions that store $t0 into array2[3] first known ...

    Briefly explain.

  5. Homework, part 4

    Submit answers to these three exercises in a single text file under the name of "hw6-circuits".

    This is not a partnership assignment. Hand in your own work.


    Last modified: Thu Oct 15 09:31:05 PDT 2009