Why does the math
module return the wrong result?
First test
A = 12345678917
print 'A =',A
B = sqrt(A**2)
print 'B =',int(B)
Result
A = 12345678917
B = 12345678917
Here, the result is correct.
Second test
A = 123456758365483459347856
print 'A =',A
B = sqrt(A**2)
print 'B =',int(B)
Result
A = 123456758365483459347856
B = 123456758365483467538432
Here the result is incorrect.
Why is that the case?
Because
math.sqrt(..)
first casts the number to a floating point and floating points have a limited mantissa: it can only represent part of the number correctly. Sofloat(A**2)
is not equal toA**2
. Next it calculates themath.sqrt
which is also approximately correct.Most functions working with floating points will never be fully correct to their integer counterparts. Floating point calculations are almost inherently approximative.
If one calculates
A**2
one gets:Now if one converts it to a
float(..)
, one gets:But if you now ask whether the two are equal:
So information has been lost while converting it to a float.
You can read more about how floats work and why these are approximative in the Wikipedia article about IEEE-754, the formal definition on how floating points work.