I have a situation when the classic representation error in Python started to be a problem: I need them to be used for Matrix operations in Numpy and the decimal type is not supported yet.
You all know that if I do 111.85 * 111.85
I will get 12510.422499999999
but if I round(12510.422499999999, 4)
I can get the proper result which is of course 12510.4225
.
But the actual questions are:
Even if you round the number, the decimal still won't be exactly what you expect it to be. Python may display only the most significant bits, but the underlying decimal inaccuracy is still there.
The python docs devote a section to it
Many users are not aware of the approximation because of the way values are displayed. Python only prints a decimal approximation to the true decimal value of the binary approximation stored by the machine. On most machines, if Python were to print the true decimal value of the binary approximation stored for 0.1, it would have to display
>>> 0.1 0.1000000000000000055511151231257827021181583404541015625
That is more digits than most people find useful, so Python keeps the number of digits manageable by displaying a rounded value instead
>>> 1 / 10 0.1
For most use cases, the inaccuracy can safely be ignored. If you are dealing with extremely small or extremely large numbers or need accuracy out to many decimal places, you can use the decimal library
>>> Decimal('111.85') * Decimal('111.85')
Decimal('12510.4225')
The real issue here is to identify how the representation error could start to be a problem in the first place.
You should not be using the representation for human-readable printing of floats, you should be using for example str.format
or some other presentation method.
Keep your numbers as numbers for all calculations (whether you do that with floats or Decimals or whatever...) and only round or truncate things at presentation.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With