We can do substantially better by simplifying our original formula to eliminate
the troublesome subtraction. Recall that our original formula was
Any algebraic simplifications that we do will be exact, since we are working
with symbols instead of with floating-point numbers. As a first step, let's
square the (R+h) term and see what happens. We obtain:
This gives us a chance to do the subtraction, cancelling out the first and last
terms and yielding
Let's work with this formula and see if it makes an improvement. First,
let's set the accuracy back to six digits as you may have changed it:
| Digits := 6; |
Now let's plug in the appropriate values for R and h:
| sqrt(2*2.09E7*38.0 + 38.0^2); |
That's much better: the six-digit answer that we get is 39,854.7 feet. If you
check back you'll notice that this is the same answer that we got with the old
formula using ten digit floating-point numbers.
Now try reducing the length of the floating-point numbers to 5, and then to 4,
and so on down to 1. What do you notice about the answers that come out?
Click here for the answer
Joseph L. Zachary
Hamlet Project
Department of Computer Science
University of Utah