Floating Point Number Representation

How to Submit Your Lab Writeup

Review the general guidelines for submitting work, especially Laboratory Exercises. You must include an Academic Honesty Statement on the top page of your lab report, and this statement must be signed by all collaborators before submission. Please number your responses and put them in numerical order so that graders don't have to search through pages trying to figure out what sections answer each question.

Hand in only the paper copy and printouts of programs created or modified. You do not need to submit electronic copies to the grader email account. Make sure that the names of both lab partners are clearly listed and be sure to label the problem number for each question. Hand in answers to the following question numbers: 1, 4, 5, 7, and 8. Some questions will require printouts of code you have written or modified.

Lab submissions handed in after 2:45 on the due date will suffer a 10% late penalty!!


This lab provides experience viewing the representation of floating-point real numbers on PC/Linux machines, and explores an application for which numerical round-off error has visible consequences.

Binary Representation of Floating-Point Numbers:

The first part of this lab asks you to review the bit-level storage of floating point numbers on PC/Linux computers.

  1. Write the real numbers ± 1.0, ± 2.0, ± 3.0, ± 6.0, and ± 9.0 using the IEEE Standard for 32-bit Floating Point Numbers.
  2. Copy the program data-rep.c to your account and compile it. Then enter real numbers, and conduct experiments to determine:
    • which bit is the sign bit,
    • which bits are used for the mantissa,
    • which bits are used for the exponent, and
    • what bias or excess is used in the storage of exponents.

    Write down the pattern that you observe.

  3. When the decimal number 0.1 (one tenth) is converted to binary, the resulting floating-point number is the repeating sequence 0.1001100110011 (just as the decimal representation of one third is the repeating sequence 0.333).

    Use data-rep.c to determine the floating-point number that is actually stored for the decimal number 0.1 (one tenth). Write down how this differs from the actual binary number.

  4. Use your knowledge of the storage of real numbers to determine what real number comes "immediately after" 3.0 and 10.0 on this system. That is, look at the mantissa to determine what change would yield the smallest number above 3.0 and 10.0. Record that number.

    Hint: When running the data-rep program, toggle (i.e. switch/slip the value from 0 to 1 or from 1 to 0) an appropriate bit and look at what results.

Floating-point Numbers and Loops

Inaccuracies in representing floating-point numbers with a limited number of digits of accuracy have an impact on how programs are written and how they run. This section of the lab explores some of these consequences.

  1. Copy float-loop.c to your account. The idea of this program is to work through a loop, starting at 0.0, incrementing by 0.1 each time through the loop, and continuing while the number is not 1.0.
    1. Read through this program. Write out what should be printed (including the expected value of sum to be printed each time).
    2. Run this program, and describe what happens.
      Note: You can stop any running program by typing ctrl+c.
    3. Review the first part of the output printed to determine why the program ran the way it did.

      Hint: After compiling the program, you might use the following line to run the program and look at the first several lines of output:

      ./float-loop | head -n 20
    4. Change the loop condition to (val <= end). Again, explain what happens. What is the last value printed within the loop? What sum is actually computed?
  2. Change float-loop.c so that the variables are declared as double rather than float, and repeat Steps 4a-d.
    • What happens this time?
    • Why?
  3. The program float-loop.c illustrates that loops may or may not repeat the number of times expected, when the variables within the loop condition are floating-point numbers. One common way to resolve this problem is to change the loop control variables to an integer. For example, for float-loop.c, we could use an int variable i to control the loop. Effectively, i has the value of 10 times the value we intend for val. The main loop might be
    int i;
    for (i = 0; i <=10; i++)
       val = i / 10.0;

    Here, the int i is always computed exactly, so the loop always runs exactly the desired number of times, and the value of val is recomputed from the exact number i each time so inaccuracies in the storage of 0.1 do not compound.

    Rewrite float-loop.c to replace the while (val < end) loop with a for construction using an integer as the loop control variable. Then run the program to confirm it produces the desired output. Include your modifed program with your lab write up.

More Floating-point Numbers and Loops

Your experience with float-loop.c illustrates that some issues arising with float numbers may be resolved with double numbers. The extra digits of accuracy sometimes can make a substantial difference. This section explores this observation further.

  1. Program double-loop.c is similar to float-loop.c, except that its variables are double, the range of numbers for the loop is 1000 to 1001, and the condition is val <= end as in Step 4c. Copy this program to your account.
    1. Read through this program. Write what should be printed (including the expected value of sum to be printed each time).
    2. Run this program and describe what happens.
    3. What, if anything, happens if the variables are changed to float type?

Computing Area Under y = x2:

[The following is an edited version of Section 5.5 from Introduction to Computing and Computer Science with Pascal by Henry M. Walker, Little, Brown, and Company, 1986 and is used with permission of the copyright holder.]

Suppose we are given a function y = f(x), and we want to find the area under the graph between x = a and x = b.
(The following figure illustrates the area under the curve between x = 1 and x = 3 when f(x) = x2.)

Area under f(x) = x^2 between x=1 and x=3

Using calculus, the exact size of this area is 8 2/3 or 8.666.


In what follows, we will not try to compute the desired area exactly. Rather, we will consider a fairly simple approach, called the trapezoidal rule, which can give good approximations to the area. In this approach, we break down a large area into small pieces and approximate each of the small pieces by a trapezoid (as shown below).

Approximating Area by Trapezoids

From geometry, we we can compute the area of a trapezoid:

Area of a Trapezoid is (b1 + b2) h / 2

Then we can approximate the entire area under the curve by adding up the areas of the trapezoids.

More precisely, we first divide the interval [a, b] into n equal pieces a=x0, x1, x2, . . ., xn=b. Then we use the pieces to divide the overall areas into trapezoids. After we compute the area of each trapezoids, we add up these small areas. The final formula is

Approximate Area = h[f(x0)/2 + f(x1) + f(x2) + . . . + f(xn-1) + f(xn)/2)]

where h = (b - a) / n and xj = a + jh for j = 0, 1, 2,  . ., n. This is the formula trapezoidal rule. (The interested reader should consult books in calculus or numerical methods for the details of this and other methods.)

To make this formula more concrete, we apply it to f(x) = x2 between x = 1 and x = 3 (as shown in an earlier figure), and we divide the interval ]1, 3] into five pieces. This gives: n = 5; a = 1; b = 3. The overall interval [1, 3] has length 2; we divide it into five subintervals of length h = 2/5 = 0.4. The x values are x0 = 1, x1 = 1.4, x2 = 1.8, x3 = 2.2, x4 = 2.6, x5 = 3. The trapezoidal rule gives:

Approx. Area = h[f(x0)/2 + f(x1) + f(x2)+ f(x3)+ f(x4)+ f(x5)/2)]
= 0.4[f(1)/2 + f(1.4) + f(1.8) + f(2.2) + f(2.6) + f(3)/2]
= 0.4]12/2 + (1.4)2 + (1.8)2 + (2.2)2 + (2.6)2 + 32/2]
= 8.72

Theoretical Accuracy of the Trapezoidal Rule

While it is hard to predict the accuracy of approximations with the trapezoidal rule, we can make several useful observations.

Practical Implications of Floating Point Error

Since floating-point numbers are not stored exactly, work with any individual floating point number may involve a small amount of error. If these numbers are combined in many arithmetic operations, such small numerical errors sometimes can come together to significantly affect results.


This part of the lab asks you to run and expand program trap-rule.c that computes area using the trapezoidal rule. You then will experiment with this program to investigate the effect of numerical errors.

  1. Copy trap-rule.c to your account, and then compile and run it.
    1. Review the program and describe how it works. For example, how the table is produced? Why does the function area_l_to_r use the variable i? Why does the computation for xvalue give appropriate values for x values in the trapezoidal rule?
    2. As noted above, the correct value of this area is 8 2/3 or 8.666 as determined with calculus. Write down how the computed approximations compare to this exact value as the number of intervals increases.

The function area_l_to_r adds terms in the Trapezoidal Rule from first to last. For the function given, the terms get steadily larger as the function is increasing from left to right. A natural question arises regarding what might happen if the terms were added in the opposite order.

  1. Modify the program to include another function area_r_to_l, which adds the terms in the Trapezoidal Rule from last to first (i.e., from the nth term toward the initial term). Then, in the main loop, add another column to the table, for "Computing from R to L".
    1. Run the revised program, showing the results of both left-to-right and right-to-left computations.
    2. Compare the results of the left-to-right and right-to-left computations. What patterns do you observe? What, if any, differences do you identify? Briefly explain what you see.
    3. If this lab is to be turned in, include your program for this step as well as your explanations and other work.