CSC 161  Grinnell College  Spring, 2010 
Imperative Problem Solving and Data Structures  
This program provides experience with programming practices that can have an impact on the accuracy of processing with real numbers.
Consider program ~walker/c/errors/infinity.c:
/* computing infinity */ #include <stdio.h> int main () { double old_sum, sum; printf ("This program adds 1.0 to a sum until the sum stops changing.\n"); sum = 0.0; do { old_sum = sum; sum += 1.0; } while (sum != old_sum); printf ("The sum stopped changing when it reached the value %f\n", sum); }
When this program is run on one particular machine, the output is:
This program adds 1.0 to a sum until the sum stops changing. The sum stopped changing when it reached the value 9007199254740992.000000
Harmonic Series, a Small Infinity: In mathematics, one can show that the sum
approaches infinity. (This sum is called the harmonic series, and this sum sometimes is said to diverge.) From this standpoint, the program harmonic should never stop.
Run this program on a computer, and explain what happens.
Convergent Series: In mathematics, the convergence of a series is defined as a limit of partial sums. In particular, we let
and then we define
With this definition, many tests for convergence and divergence are well established. One theorem states:
The harmonic series shows, however, that the converse of this theorem is not true.
Comparison of Algorithms: Consider the following problem. After a fly settles on a table, a person tries to hit the fly with a flyswatter. In particular, the person starts with the flyswatter 1 yard from the table and the person moves the flyswatter at the rate of 1 yard per second. Does the person hit the fly and if so when? (You may assume that the fly remains still and the person has good aim.)
Now consider two solutions.
Plato's Paradox: We divide time into various intervals. In the first time interval, the flyswatter moves from 1 yard to 1/2 yard from the table. In the second interval, the flyswatter moves half the remaining distance, from 1/2 to 1/4 yard. In the third interval, the distance is again halved. In subsequent time intervals, this halving of distance continues. Because the distance remaining is always halved, the flyswatter never reaches the table.
Another Approach: The flyswatter travels 1 yard per second. Thus, in 1 second, the flyswatter travels 1 yard. Since this is the distance from the initial position of the flyswatter to the table, the flyswatter must hit the table (and thus the fly) in 1 second.
Consider a circle of radius 2, and consider the area within the circle that lies in the first quadrant. Since the radius is 2, the area of the entire circle is π2^{2} or 4π, and the area in the first quadrant is 1/4 of this area or π.
Within the first quadrant, the function describing this circle is f(x) = sqrt(4x*x).
One way to approximate the area of this quarter circle is to divide the xaxis into n intervals, each of width 2/n. This gives points:
For each point x_{i}, we consider a rectangle with height on the circle (f(x_{i})) and width (2/n). Adding the rectangles together, we get an approximation to the area in the first quadrant:
Dividing the interval [0,2] into more intervals (increasing n) gives successively good approximations.
This document is available on the World Wide Web as
http://www.cs.grinnell.edu/~walker/courses/161.sp10/labs/labnumerrors.shtml
created 4 May 2008 last revised 22 February 2009 

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