In this approximate research algorithm, I set epsilon to 0.01 and step to 0.0001.
The run outcome is:
ans = 0.9949999999999067.
Since ans
adds 0.0001 each step, the outcome should be precise to fourth digit after the point.
How come it has so many digits?
Code is as follows:
x = 1
epsilon = 0.01
step = epsilon**2
numGuess = 0
ans = 0.0
while abs(ans**2 - x) >= epsilon and ans <= x:
ans += step
numGuess += 1
print('numGuess =', numGuess)
if abs(ans**2 - x) >= epsilon:
print('Failed on square root of',x)
else:
print(ans, 'is close to square root of',x)