0

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)
Annie
  • 1
  • 1
  • Possible duplicate of [Is floating point math broken?](https://stackoverflow.com/questions/588004/is-floating-point-math-broken) – Sneftel Jul 16 '18 at 14:19

2 Answers2

1

Your software does not use decimal for floating-point arithmetic. It uses binary-based floating-point. The string “.01” in your source code is converted to binary-based floating-point, resulting in a value that is close but different.

Therefore the results of the computations you ask about are near simple decimal values but are different. Printing the values with many decimal digits reveals those differences.

Eric Postpischil
  • 195,579
  • 13
  • 168
  • 312
0

No, due to your implementation, you can at most expect 2 digits for the final answer of your square root approximation. Even if you compute with exact numbers (i.e. we simply ignore floating-point inaccuracies), you get

NumGuess   ans        |ans**2 - 1|
9949       0.9949     0.01017399 
9950       0.9950     0.009975

So the theoretical answer would be 0.9950 which would be rounded to the double value 0.99499999999999999555910790149937383830547332763671875.

If we compute with actual floating point values, see Is floating point math broken?

gammatester
  • 1,131
  • 1
  • 8
  • 12