pythonpython-3.xnumpybit-representation

Rounding floats with representation error to the closest and correct result


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:


Solution

  • 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')