
>>> 1.31-1.0
0.31000000000000005
>>> 1.5-1
0.5
>>> 1.3-1
0.30000000000000004
>>> 1.2-1
0.19999999999999996
>>>
虽然误差很小,但是这只是普通的减法啊!!!
能不能解决啊?
根据目前的浮点运算的标准, 这样的计算结果是正确的.afox800 写了:python
>>> 1.31-1.0
0.31000000000000005
>>> 1.5-1
0.5
>>> 1.3-1
0.30000000000000004
>>> 1.2-1
0.19999999999999996
>>>
虽然误差很小,但是这只是普通的减法啊!!!
能不能解决啊?
代码: 全选
if (0.8 - 0.6) - 0.2 <= sys.float_info.epsilon:
print('0.8 - 0.6 == 0.2')
else:
print('Not equal')
代码: 全选
Python 3.4.0 (default, Apr 27 2014, 23:33:09)
[GCC 4.8.2 20140206 (prerelease)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> print(1.3-1)
0.30000000000000004
>>> 1.3-1 == 0.3
False
>>> from decimal import Decimal
>>> print(Decimal('1.3')-Decimal('1'))
0.3
>>> Decimal('1.3')-Decimal('1') == Decimal('0.3')
True