Newton's Method:
Newton's Method is used to find successive approximations to
the roots of a function. If the function is y = f(x)
and x is close to a root, then we usually
expect the formula below to give _{0}x
as a better approximation. Then you plug the _{1}x
back in as _{1}x and iterate.
See Newton's Method for
a derivation of the formula below._{0}
The Reciprocal is Enough:
Suppose one wants to do division, say by a number d.
It suffices to calculate the reciprocal of d, that is,
1/d. Then c/d is calculated as c*(1/d).
Background:
Historically, Newton's method has been used in software and
in hardware for two impressive tasks: to
compute the square root, and (surprisingly) to carry
out division. Early in the computer era
addition (and subtraction, of course),
along with multiplication, were implemented in hardware.
Division was quite a bit harder to implement. Think about
the multiplication and division by hand that you (surely)
learned early in school. Programming multiplication would be relatively
straightforward compared with division, which involved
successively guessing the next digit to use for
the quotient. (Many people now say that they can no longer
do division by hand.) To quote from Knuth (Vol. 2, page 251):
Double-precision floating division is the most difficult routine, or at least the most frightening prospect we have encountered so far in this chapter. Actually, it is not terribly complicated once we see how to do it; ... Because of such difficulties, Newton's method was often used. Newton's Formula for the Reciprocal of d:
In order to calculate 1/d, use the function
f(x) = 1/x - d, with 1/d as its root.
Using Newton's method, one gets the equations:
Or just
As with the formula for square roots, this is an amazingly
simple formula, given that it produces such good results.
Notice also that the formula has only two multiplications
and one subtraction. But the overwhelmingly important
property of this formula is that it
The Normalization Step:
Start with a number d, where we want 1/d.
First normalize the input to some
small range using powers of 2. With binary floating
point numbers, multiplication or division by a power of two
only needs to adjust the exponent part of the number, which
can be done very quickly.
For the reciprocal, normalize into a value
The Approximation Function:
A Program for Reciprocal:
The program below incorporates normalization and calculates
the reciprocal of an arbitrary positive double.
The important issue here is that this method does not
use division, except for division by a power of two,
which can be carried out very efficiently on a binary machine.
An actual commercial software division routine would have many
improvements and optimizations. The routine below only
demonstrates the feasibility.For convenience the input double is given as a command line argument. In the several example outputs, there is one very small positive number and one very large one.
In the data below, The link Division and Square Root compares the Newton's Method algorithm for division with the one for square root, showing remarkable similarity. The program below used the final linear approximating function (the black line above). I ran this program using the earlier approximating function (the green line above) with the same four inputs. The initial values were quite different, but the final values differed only in trivial ways.
( |