Now that we've finished our excursion into the details of floating-point
numbers, let's return to the implementation of the Kitty Hawk problem.
Referring to the diagram that we drew when creating our model, and letting R
stand for the radius of the earth and h for the height of Kitty Hawk, we can
see that we must do the following computation to find the distance that we can
see out into the sound:
This is, of course, a direct consequence of the Pythagorean Theorem, which is
the basis for our method of solving this problem.
Since we know the value of R to three significant digits, and the value of
h to two significant digits, it certainly seems that we should be able to get
an accurate answer by using six digits of precision in our floating point
computations. Let's tell Maple to use six digits:
| Digits := 6; |
Now let's do the computation by plugging in for the values of R and h.
Notice that Maple has a built-in function that will compute a square root:
| ((2.09e7 + 38.0)^2 - 2.09e7^2); |
That answer doesn't look quite right, does it? What do you suppose went wrong?
Click here for the answer
What can we do to get around this problem? One approach is to simply ask Maple
to use more digits of floating point accuracy. Try increasing the number of
digits to 7, then to 8, and so on until you reach 10. The answer will
eventually settle down into something more reasonable looking.
Although it worked in this case, increasing floating-point accuracy until
reasonable answers appear is not always a good idea. Here are a few reasons
why:
- Although Maple allows you to control the exact number of digits used for
floating point computations, this is not generally the case with pocket
calculators and programming languages such as C and Fortran.
- It takes you, working by hand, much longer to multiply a pair of
ten-digit numbers than it does a pair of six-digit numbers. This is also the
case for Maple as well as for other programming languages. Such a difference
can become extremely important when you write a program that does millions of
floating point multiplications, instead of just a handful.
- There is something computationally suspicious about being forced to use
ten digit floating point numbers to manipulate numbers that have three or fewer
significant digits. In fact, we could argue that it is meaningless to add
Kitty Hawk's height to the earth's radius. Because the last five digits of the
radius are completely unknown, the radius simply becomes lost in the noise.
The root problem is this: it is a bad idea to expect the difference between two
numbers that differ only in their non-significant digits to be computationally
meaningful. The answer that you obtain from such a computation depends in a
large way upon floating point error. Such a computation is called
unstable, and unstable computations should be avoided whenever possible. In
this case, fortunately, it is possible.
Joseph L. Zachary
Hamlet Project
Department of Computer Science
University of Utah