Jan-28-2018, 11:04 PM
This is a limitation of binary floating point arithmetics. The python number 0.0952 is not exactly equal to the real number 952/10000 because 0.0952 is stored in base 2 and only a finite number of bits are kept. In other words it cannot be represented exactly in memory at the machine precision. You can see the error with the fractions module
The consequence for you is that
>>> from fractions import Fraction >>> Fraction(0.0952) - Fraction(952, 10000) Fraction(77, 11258999068426240000)you see that there is an error. This is not a Python specific error: you would get the same behavior in all languages or systems that use 64 bits floating point numbers.
The consequence for you is that
B*100
is not exactly equal to 9.52>>> x = 0.0952 >>> x * 100 9.520000000000001By printing with the
format()
method, you don't see this error because the format method performs a rounding when transforming the number to a string>>> '{:.2f}'.format(x * 100) '9.52'