CSC 161 Grinnell College Spring, 2009
Imperative Problem Solving and Data Structures

Numerical Errors


This program provides experience with programming practices that can have an impact on the accuracy of processing with real numbers.

Steps for this Lab

  1. Consider program ~walker/c/errors/infinity.c:

       /* computing infinity */
       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
    1. Explain this result.
    2. What happens if this program is run, changing old_sum and sum from double to float? (Remember to change the format for printing from lf to f.)
  2. Harmonic Series, a Small Infinity: In mathematics, one can show that the sum

    1 + 1/2 + 1/3 + 1/4 + ...

    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.

  3. Convergent Series: In mathematics, the convergence of a series is defined as a limit of partial sums. In particular, we let

    Sn  = Σni=1 ai

    and then we define

    Σi=1ai = limn→∞ Sn

    With this definition, many tests for convergence and divergence are well established. One theorem states:

    If Σi=1ai  converges, then limn→∞ an = 0.

    The harmonic series shows, however, that the converse of this theorem is not true.

    1. Use what you know about numerical errors to prove that the converse is true if the partial sums are evaluated by computer.
    2. Find a series in which partial sums converge on your computer but in which limn→∞ an ≠ 0.
  4. 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.

    1. Explain the apparent contradictory conclusions of these algorithms.
    2. Write programs that implement each of these algorithms and compare the output of the programs.
  5. 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 π22 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(4-x*x).

    One way to approximate the area of this quarter circle is to divide the x-axis into n intervals, each of width 2/n. This gives points:

    x1=0, x2=0+(2/n), x3=0+2(2/n), x4=0+3(2/n), ... , xi=0+(i-1)(2/n), ... , xn=0+(n-1)(2/n)

    For each point xi, we consider a rectangle with height on the circle (f(xi)) 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.

    1. Write a program that uses approximations by rectangles to estimate the area within this circle. The program should read a value for n and then compute the sum of the rectangles.
    2. Since the circle decreases in height from 0 to 2, the size of the rectangles decreases as x increases. Modify the program, so it computes rectangles from left to right and also from right to left. The program should print both estimates.
    3. What happens when n increases? For example, what happens if n goes from 1,000 to 10,000 to 100,000 to 1,000,000 to 10,000,000? Do errors get smaller, remain the same, or get bigger?

This document is available on the World Wide Web as

created 4 May 2008
last revised 31 January 2009
Valid HTML 4.01! Valid CSS!
For more information, please contact Henry M. Walker at