python 浮点数计算的问题
原来没太注意浮点数计算会有问题,记录下
from decimal import Decimal def calc(a,b): c = 2*(a+b) s = a*b print(c, s, sep='____________') calc(Decimal('1.5'),Decimal('2.3')) calc(1.5,2.3)
运行结果:7.6____________3.45 7.6____________3.4499999999999997
上面将字符串转成数来进行计算,当然Decimal也可以精确浮点数。