浮点数无法表达出所有的十进制小数位:
a = 2.1
b = 4.2
c = a + b
c
6.300000000000001
(a + b) == 6.3
False
解决方法可以使用decimal模块:
from decimal import Decimal
a = Decimal('4.2')
b = Decimal('2.1')
a + b
Decimal('6.3')
print(a + b)
6.3
(a+b) == Decimal('6.3')
True
浮点数无法表达出所有的十进制小数位:
a = 2.1
b = 4.2
c = a + b
c
6.300000000000001
(a + b) == 6.3
False
解决方法可以使用decimal模块:
from decimal import Decimal
a = Decimal('4.2')
b = Decimal('2.1')
a + b
Decimal('6.3')
print(a + b)
6.3
(a+b) == Decimal('6.3')
True