CSC 161  Grinnell College  Fall, 2011 
Imperative Problem Solving and Data Structures  
This lab provides experience viewing the representation of floatingpoint real numbers on PC/Linux machines, and explores an application for which numerical roundoff error has visible consequences.
You should read IEEE floatingpoint representations of real numbers by John Stone.
The first part of this lab asks you to review the bitlevel storage of floating point numbers on PC/Linux computers.
Write the real numbers ± 1, ± 2, ± 3, ± 6, ± 9 using the IEEE Standard for 32bit Floating Point Numbers..
Copy the program ~walker/c/datarep.c to your account and compile it. Then enter real numbers, and conduct experiments to determine:
[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) = x^{2}.)
In our solution, 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).
From geometry, we we can compute the area of a trapezoid:
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=x_{0}, x_{1}, x_{2}, . . ., x_{n}=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(x_{0})/2 + f(x_{1}) + f(x_{2}) + . . . + f(x_{n1}) + f(x_{n})/2)]
where h = (b  a) / n and x_{j} = 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) = x^{2} 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 x_{0} = 1, x_{1} = 1.4, x_{2} = 1.8, x_{3} = 2.2, x_{4} = 2.6, x_{5} = 3. The trapezoidal rule gives:
Approx. Area  = h[f(x_{0})/2 + f(x_{1}) + f(x_{2})+ f(x_{3})+ f(x_{4})+ f(x_{5})/2)] 
= 0.4[f(1)/2 + f(1.4) + f(1.8) + f(2.2) + f(2.6) + f(3)/2]  
= 0.4]1^{2}/2 + (1.4)^{2} + (1.8)^{2} + (2.2)^{2} + (2.6)^{2} + 3^{2}/2]  
= 8.72 
While it is hard to predict the accuracy of approximations with the trapezoidal rule, we can make several useful observations.
The trapezoidal rule relies upon the actual area under the graph being close to the area under the trapezoid.
Since floatingpoint 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 write (or modify) a program that computes area using the trapezoidal rule in various ways. You then will experiment with this program to investigate the effect of numerical errors.
Write a simple C program (without functions) that does the following:
The program must use float (NOT double) variables for all real numbers (as this will highlight numerical error issues).
Note: The Introduction to C Through Annotated Examples gives several versions of the trapezoidal rule, using functions and other fanciness. While you are welcome to use these examples as a base, your code should be simpler. Also, your code will need to perform the computations in several ways, as noted above.
This document is available on the World Wide Web as
http://www.cs.grinnell.edu/~walker/courses/161.fa11/labfloats.shtml
created 17 September 2001 last revised 6 October 2011 

For more information, please contact Henry M. Walker at walker@cs.grinnell.edu. 