概要:浮点数在计算机中以二进制保存的,将会导致计算不精准
在python中运行如下代码,结果如下
a=1.1
b=2.2
a+b
为解决此问题,需引入 decimal 模块。
from decimal import Decimal
a=Decimal('1.1')
b=Decimal('2.2')
a+b
ps:必须为’1.1’,单引号不可省
设置精度
from decimal import *
getcontext().prec = 6
Decimal(1)/Decimal(7)