21 September 2012
What is the 8-bit 2's complement encoding (in binary and hexidecimal) for the following numbers? 0, 1, 2, -1, -2, 127, -127, -128
Write a C or C++ program that takes two single-precision floating point numbers on the command line and prints their product. However, the only place you can use an actual floating point variable or computation is to print the result. You will find a file named 'product.c' in your hw2 directory that does this using floating point operations. You must replace the marked section of this file with something that does the same thing using only integer operations. This includes generating the floating point encoding for the operands from the string input, performing the multiply, and generating the encoded result.
int result; /* ... your code to compute the result ... */ printf(" x = %g (encoding 0x%08x)\n", *(float*)&x, x); printf(" y = %g (encoding 0x%08x)\n", *(float*)&y, y); printf(" result = %g (encoding 0x%08x)\n", *(float*)&result, result);
You should take scientific notation input as strings on argv[1] and argv[2] and encode them using the 32-bit IEEE floating point format. You do not need to support the full floating point text representation, but should handle:
You SHOULD handle NaN, infinity, and denormalized floats.
For reference, wikpedia page for IEEE single precion float has a pretty good summary of the format.
Some examples:
product 1e0 -1e0
x = 1 (encoding 0x3f800000)
y = -1 (encoding 0xbf800000)
result = -1 (encoding 0xbf800000) product 5e-1 1e1 x = 0.5 (encoding 0x3f000000)
y = 10 (encoding 0x41200000)
result = 5 (encoding 0x40a00000) product inf 1e0 x = inf (encoding 0x7f800000)
y = 1 (encoding 0x3f800000)
result = inf (encoding 0x7f800000) product inf 0e0 x = inf (encoding 0x7f800000)
y = 0 (encoding 0x00000000)
result = nan (encoding 0xffc00000) product nan 2e10
x = nan (encoding 0x7fc00000)
y = 2e+10 (encoding 0x509502f9)
result = nan (encoding 0x7fc00000)
Submit your answer to part 1 in a ascii text file named "hw1.txt". Submit your solution to the part 2 as a single C or C++ file named 'product.c' or 'product.cxx'.
All electronic submissions in this class will be done using the CVS version control system. You should look at the class CVS instructions before you start work. To get full credit for your submission, you should (1) check out a copy of the empty hw1 directory before you start, (2) do your work in that checked out copy, (3) submit several intermediate checkins with short but useful messages, and (4) check in your final submission before class starts on the day of the deadline