Python floating point number operation error problem

floating point arithmetic error overflow

[in]:    a = 1.1

          b = 2.2

          a + b

[out]:  3.3000000000000003


Solution

Use the decimal module provided by Python to convert floating-point numbers to strings first, and then perform operations

from decimal import Decimal
a = Decimal('1.1')
b = Decimal('2.2')
a + b
>>>Decimal('3.3')

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325383665&siteId=291194637