CPSC/MATH 402 - Computer Exercises

Log into the Linux system to do this exercise. This handout and programs are on-line -- go to http://cs.roanoke.edu/~cpsc/Spring2004/CPSC402A and follow the link.

Investigating Floating Point Representation and Arithmetic

Investigate the floating point representation on the Linux system by doing the following:
  1. The program floatRep.cc is a C++ program that determines approximations of unit roundoff (machine epsilon) for type float and type double using two different techniques. One is the technique described in Computer Problem 1.3 on page 44 of the text; the other is the alternate characterization of machine epsilon described on page 20 (as the smallest positive number epsilon such that fl(1 + epsilon) > 1). The program FloatRep.java is a Java version of the same thing.

    Do the following:

    1. Save the programs to your directory (you can do this with the File/Save As feature in the browser or copy and paste).
    2. Compile and run the C++ version. In case you forgot, the commands are:
              g++ floatRep.cc -o epsilon
      to compile and put the executable in a file named epsilon. Just type ./epsilon to run (you need the ./ to refer to your current directory). Alternately, use the command
             g++ floatRep.cc
      to compile then ./a.out to run.
    3. Study the output. Compare the different answers for the different approximation techniques. How do they compare to theory (for IEEE floating point representation)? NOTE: If you want a printed copy of the output, redirect to a file then print the file. The following commands do that if the name of the executable is epsilon and the name of the file is eps.out:
                ./epsilon > eps.out
                nenscript -2rG eps.out

    4. Now compile and run the Java version. The commands to do this are as follows:
                javac FloatRep.java      -- compiles to bytecode
                java FloatRep            -- interprets/runs the bytecode
    5. Compare these results to those of the C++ program.

    6. Add to one of the programs above (you choose) code to determine the smallest power of two that is stored in both double and single precision (UFL). Print both the decimal form of the power and the exponent.

    7. Add a calculation in the program (and print the result -- the calculation can be in the print statement) that will result in NaN.

    8. Now add code to find the largest power of 2. (Or even better - OFL -- See Computer Problem 1.2 on page 44)

  2. The program Problem19.java is part (a) and (b) of Computer Problem 1.9 in the text (page 46).
    1. Save the program to your directory, compile and run it using the values in part (c). How did it do?

    2. Do part (d) -- that is, modify the program so it gets accurate results for x < 0. Don't get carried away -- there isn't much you need to do!

HOMEWORK DUE Thursday, February, 2004:

  1. Answer questions (a) and (c) in Computer Problem 1.3, page 44.
  2. Determine from the results of your calculation of UFL whether or not gradual underflow is used on this system. Justify your answer by computing theoretically what the smallest number would be without subnormals and what the smallest positive number would be with them. (HINT: You need to use the info about IEEE Double precision floating point representation.)
  3. What did you get for the largest positive double? How does it compare to the theoretical value (based on the formula in the book)?
Also hand in a copy of your program that computed UFL and the largest positive double and the revised Problem19.java.