The algorithm is given as follows:
Begin by guessing that the square root is x / 2. Call that guess g.
The actual square root must lie between g and x/g. At each step in the successive approximation, generate a new guess by averaging g and x/g.
Repeat step 2 until the values of g and x/g are as close together as the precision of the hardware allows. In Java, the best way to check for this condition is to test whether the average is equal to either of the values used to generate it.
My question is in relation to steps 2 and 3. Why do we make the new guess (g + x/g) / 2 and why do we compare this result to g and x/g to determine whether or not to terminate our loop ?