In Python 2.6.5 the following expression yields False:
在Python 2.6.5中,以下表达式产生False:
>>> import decimal
>>> decimal.Decimal('0') < 1.0
False
Is there a rationale explaining why comparison of Decimal against float should behave like this?
是否有理由解释为什么Decimal与float的比较应该像这样?
1 个解决方案
#1
13
From the documentation of the decimal module:
从十进制模块的文档:
Changed in version 2.7: A comparison between a float instance x and a Decimal instance y now returns a result based on the values of x and y. In earlier versions x < y returned the same (arbitrary) result for any Decimal instance x and any float instance y.
版本2.7中更改:浮点实例x和Decimal实例y之间的比较现在返回基于x和y值的结果。在早期版本中,x
为任何decimal实例x和任何float实例y返回相同(任意)的结果。
So it looks like that was a bug/missing feature and all you need to do is upgrade.
所以看起来这是一个错误/缺失的功能,你需要做的就是升级。
#1
13
From the documentation of the decimal module:
从十进制模块的文档:
Changed in version 2.7: A comparison between a float instance x and a Decimal instance y now returns a result based on the values of x and y. In earlier versions x < y returned the same (arbitrary) result for any Decimal instance x and any float instance y.
版本2.7中更改:浮点实例x和Decimal实例y之间的比较现在返回基于x和y值的结果。在早期版本中,x
为任何decimal实例x和任何float实例y返回相同(任意)的结果。
So it looks like that was a bug/missing feature and all you need to do is upgrade.
所以看起来这是一个错误/缺失的功能,你需要做的就是升级。