# 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:

• saying #include <math.h> in your source file;
• giving the option -lm (that's minus L M) at the end of your gcc command. This tells the linker to include the math library, something that doesn't happen by default.

#### 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 ...

• by the C compiler,
• by the assembler, or

Briefly explain.

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

• by the C compiler,
• by the assembler, or

Briefly explain.

5. ### Homework, part 4

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

• Combinational circuits implement boolean functions. There are four boolean functions of one variable. They are shown in the following table.

 i False(i) Identity(i) Not(i) True(i) 0 0 0 1 1 1 0 1 0 1

How many Boolean functions of two variables are there? Make the table. Give names to the functions that you recognize. (Hint: you've seen AND, OR, NOT, XOR so far.)

• In lab we have worked with AND, OR, and NOT. This set of Boolean operators is complete in that any Boolean functions can be implemented with a combination of these operators. In fact, there are some very useful operators that are complete all by themselves. One of them is NAND. Its symbol is

And its truth table is

```a   b   a nand b
0   0      1
0   1      1
1   0      1
1   1      0
```

Show how to build AND, OR, and NOT using only NAND. This constitutes a proof that NAND is complete.

• A multiplexor (often referred to as "mux") is an extremely useful circuit. The MIPS CPU has several multiplexors, as we will see in a couple of weeks. A multiplexor takes three inputs, a, b, and s (a selector). The output is a if s is 0 and b if s is 1. It's diagrammed below.

Provide the truth table of a multiplexor, and give a Boolean expression that relates its output to its inputs.

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