3.2 DIY Floating Point Addition
In C, write a program that reads pairs of 32-bit floating point numbers from standard input and adds them together using only C’s integer operations, then displays the result.
You only need to deal with positive floating point numbers with no more than six (6) significant digits and between +10^-37 and +10^37.
Your program should expect any number of pairs of numbers. Each element of the pair should be delimited by a space character ( ) and each pair should be delimited by newline characters (\n). The addition of each pair should be printed one-by-one. When the input to the program ends, the program should exit without errors.
The representation must be displayed in the following format, as demonstrated with the input 34.5 1.250:
Float 1: 34.500000 |
bits: 0 1000 0100 000 1010 0000 0000 0000 0000 |
Float 2: 1.250000 |
bits: 0 0111 1111 010 0000 0000 0000 0000 0000 |
Hardware: 35.750000 |
bits: 0 1000 0100 000 1111 0000 0000 0000 0000 |
Emulated: 35.750000 |
bits: 0 1000 0100 000 1111 0000 0000 0000 0000 |
|
The program should have no other output.
A README file that contains a short write-up of your project. It should discuss the approach you took to solving the problem, it should discuss any problems or issues that still remain, it should discuss any interesting things that you learned in the course of the project. Finally, it should provide the grade you believe the project deserves with a detailed explanation of why. This explanation should almost certainly focus on why you believe the program is correct.
This file must be in plain-text. If you need formatting, use Markdown. It must be limited to 80 characters wide, unless you are quoting program output.
Your source code, named a2.c.
A test input file, named test.in, that contains example input. It must contain at least ten (10) pairs. In addition to four (4) pairs of your choosing, it must contain these pairs:
34.5 1.25
2.75e3 256.0
1.4e-3 13.0
7.491 4.617e20
9643.0 1.237e-2
25e37 15e37
A test output file, named test.out, that contains the expected output for the test data.
A Makefile that first builds the program and then tests it by running it with test.in as the input and compares the actual output to test.out.
This assignment will be graded based on its correctness and your ability to articulate why it is correct.
Here’s some advice about how to do this: First, copy the mantissa values and expose the hidden bits so you have a 24-bit value rather than a 23-bit value. Second, shift the mantissa of the smaller value while decrementing the exponent until the exponents are equal. Third, add the mantissa values. Fourth, construct a new emulated float with the added mantissa and the common exponent. These first four steps are just a matter of C bit-twiddling. The hard part will come from dealing properly with removing the hidden bit, dealing with carrying during addition, and ensuring that you shift values correctly.