CS 473 - HW2

Floating Point in C

Due Wednesday, February 5, 2003

For this problem, I want you to perform a floating point addition in C. By hand: you can't use the float or double data types, either explicitly or implicitly.

Remember these coding standards apply to this assignment.

Input

Your input will consist of two numbers in human readable decimal format, on the command line. For purposes of this assignment, "human readable decimal format" means:
  1. an optional "-" sign
  2. One or more digits from 0 to 9 inclusive. If the leading digit is a 0, it'll be the only digit.
  3. A decimal point.
  4. One or more digits from 0 to 9 inclusive.

So here are some examples of valid numbers: 10.0, -13.125, 0.12500.

And here are some examples of invalid numbers: 01.125 (leading 0 isn't the only digit to the left of the decimal point), +12.375 (leading + sign), .375 (no digit before decimal point), -1. (no digit after decimal point), 2 (no decimal point).

So, an example of a valid invocation of this program is

% add 1.375 -2.0

Note that the inputs have to be on the command line: do not prompt for them, do not read them from standard input.

You don't need to check the input: we will only give your program inputs corresponding to these specifications.

Algorithm

Your program should do the following:

  1. Convert the inputs to IEEE floating point format (do this in your program, not using strtoul or other system functions). Since the computer works in binary, you'll need to use the multiplication method to convert the integer part and the division method to convert the fraction part. Print the IEEE representations to standard out.
  2. Add the two IEEE floating point numbers together, using the floating point addition algorithm given in class.
  3. Print the result to standard out, on a new line.
  4. Example

    For the example inputs given, the result should appear as follows:

    
    % add 1.375 -2.0
    3fb00000 c0000000
    bf200000
    
    

    Notes

    1. The input numbers may be positive, may be negative, or may have one positive and one negative. The result may be either positive or negative.
    2. Either number may have the greater magnitude.
    3. The only numbers you will have to deal with (either as input or as output) will be normalized. So there will be no 0's, no denorms, no infinities, and no NaNs.

    Last modified: Tue Feb 4 19:24:06 MST 2003