Combine your solutions to the two problems described below into a file named hw6 for submission to Expertiza. A submission is due at 11:59 on Monday, March 9.
This is not a partnership assignment. Hand in your own work.
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
Find a value representable in representation B and not in representation Q. Defend your answer.
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 |
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.
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 you submit to Expertiza.