No, not usually. With the inherent inaccuracy of floating point this risks creating a non-terminating loop. This next example shows a while loop.
is a way to compute the square root of
n is the number and that
is an approximation to the square root of
x' =(1/2)(x + n/x)
x' is an even better approximation to the square root.
The reasons for this are buried in your calculus book
(which you, perhaps, have buried in the darkest corner of
But, to make the formula plausible, look what happens if
x happens to be exactly the
In other words, what if
x = n0.5.
x' = (1/2)(x + n/x) = (1/2)( n0.5 + n/n0.5 ) = (1/2)(n0.5 + n0.5) = n0.5
x reaches the exact value, it stays fixed at that
n == 4 and that our first
approximation to the square root is
x == 1.
Use the formula to get the next approximation:
x' =(1/2)(1 + 4/1)