Winter 2004 UCLA CS 31 (Shinnerl)

Programming Project 4: Fixed-Point Binary Calculator

DUE: Mon. 2/9/2004

MODIFIED Specification 10AM, 1/31

1/31/04 The hexadecimal and decimal I/O are now optional, as are the implementation of multiplication and division. These features may be considered in Project 5.

I. Overview

A fixed-point representation of a number uses separate digit fields for the integer and fractional parts of the number. For example, the binary, fixed-point representation of 3.25 = 10/4 on a 10-digit simulation (5 digits each for integer and fraction) looks like this --- 00011.01000 --- except that the binary point in the middle is not actually stored.

Overflow occurs when the absolute value of the number is too large for the number of integer bits available to represent its integer part. Underflow occurs when the number is too close to zero for the number of fraction bits available to represent its fractional part. Both overflow and underflow are possible for both positive and negative numbers.

Implement a program to simulate a binary, fixed-point calculator of user-specified precision, up to a maximum of 64 binary digits each for the integer and fractional number fields. Support simple addition, subtraction, multiplication, and division of both nonnegative and negative numbers.

II. Details

  1. All input to the program must be assumed to be in fixed-point binary format of the requested precision, with binary point separating integer and fractional parts, unless, optionally, the user switches to hexadecimal mode, in which case, the input is in fixed-point hexadecimal format. Examples are given above (binary) and below (hex).
  2. When the user enters 'd' for decimal mode, his/her input will still be in either binary or hex, and the calculator works the same as before. However, in this mode, all binary (or hex.) output is accompanied by the equivalent decimal values and the suffix dec, both in parentheses. E.g.,
                     00011.01000    (    3.250  dec )
                 +   01010.01100    (   10.375  dec )
                 =   01101.10100    (   13.625  dec )
    
    Conversion from binary (or hex.) to decimal can always be done exactly.
  3. Perform addition and subtraction exactly, in binary, on the given bit sequences, reporting overflow whenever it occurs.
  4. Perform multiplication and division as follows.
    1. Approximate given fixed-point input data values as accurately as possible with floating-point values.
    2. Do the calculation (* or /) on the floating-point data approximations.
    3. Convert the floating-point result back to fixed point as accurately as possible, reporting any overflow or underflow.
  5. For simplicity, always give the integer field and the fraction field of any fixed-point value the same number of bits. This number will be called the precision of the representation and is provided by the user at start-up.
  6. Use C-style strings to represent binary fixed-point values.
  7. There is no reason to explicitly store the binary point, so don't.
  8. Remember that converting between binary (base 2) and hexadecimal (base 16) is easy: every 4 binary digits correspond to exactly one hexadecimal digit (0,1,...,9,a,b,c,d,e,f). E.g., the hexadecimal representation of the binary number 11 0011 1010 1001.0111 1101 0110 0000 is just 33a9.7d60.
  9. For convenience, always output binary digits in 4-digit chunks separated by spaces, as in the preceding example. Similarly, ignore spaces between digits in user input.
  10. The program runs as follows.
          Print a short menu explaining how to operate the program.
          Request and read in the user's requested number of digits 
             (precision) for each number's integer and fraction fields.
          Request and read in a numerical value, x,
          as a binary fixed-point number.
    
          REPEAT
             
             Prompt for and read in an operator (q,m,c,p,h,b,d,+,-,*, or /).
             The prompt may be simply a list of possible inputs: qmcphbd+-*/>.
             If      the operator is a 'q' or 'Q', terminate execution (quit).
             Else If the operator is an 'm', redisplay the short menu 
                     explaining how to operate the program.
             Else If the operator is a 'c', (re)set x to zero. 
             Else If the operator is a 'p', read in a new precision 
                     and reset all current data values to the new precision.
             Else If the operator is an 'h', switch to hexadecimal I/O format.
             Else If the operator is an 'b', switch (back) to binary I/O format.
             Else If the operator is a 'd', print decimal approximations alongside
                     all binary values until 'd' is reentered ('d' is a toggle).
             Else    display an appropriate error message and continue.
    
             Request and read in a second numerical value, y,
             as a binary fixed-point number.
    
             Perform the indicated binary operation and overwrite
               x by its value, displaying the result to the screen
               as a complete calculation:
               
                                <x-value> 
                   <operator>   <y-value> 
                             =  <new-x-value> 
               
    UNTIL DONE
  11. OPTIONALLY, you may enable a separate fixed-point decimal mode (command 'D') which accepts fixed-point decimal input. This extension is not required but may be useful for debugging. Note that conversion from decimal to binary typically incurs rounding error, because the binary expansion of one tenth does not terminate.

III. Sample I/O

...will be posted separately in a couple of days.


Project 4 Home Page