Rounding floats with representation error to the closest and correct result

前端 未结 2 865
温柔的废话
温柔的废话 2021-01-12 12:27

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

2条回答
  •  再見小時候
    2021-01-12 13:05

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

提交回复
热议问题