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
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
By printing with the
1 2 3 |
>>> from fractions import Fraction >>> Fraction( 0.0952 ) - Fraction( 952 , 10000 ) Fraction( 77 , 11258999068426240000 ) |
The consequence for you is that
B*100
is not exactly equal to 9.521 2 3 |
>>> x = 0.0952 >>> x * 100 9.520000000000001 |
format()
method, you don't see this error because the format method performs a rounding when transforming the number to a string1 2 |
>>> '{:.2f}' . format (x * 100 ) '9.52' |