补充上面关于 cmp() 函数的问题,官方文档中有如下描述: (If you really need the cmp() functionality, you could use the expression (a > b) - (a < b) as the equivalent for cmp(a,b).)
补充上面关于round()函数的问题,注意下面例子:
>>> round(1.05,1)
1.1
>>> round(1.15,1)
1.1
>>> round(1.55,1)
1.6
>>> round(2.05,1)
2.0
>>> round(2.15,1)
2.1
>>> round(2.55,1)
2.5
官方的解释是:这不是bug,而事关浮点数存储:
>>>fromdecimalimportDecimal
>>>Decimal.from_float(1.05)
Decimal('1.0500000000000000444089209850062616169452667236328125')
>>>Decimal.from_float(1.15)
Decimal('1.149999999999999911182158029987476766109466552734375')
>>>Decimal.from_float(1.55)
Decimal('1.5500000000000000444089209850062616169452667236328125')
>>>Decimal.from_float(2.05)
Decimal('2.04999999999999982236431605997495353221893310546875')
>>>Decimal.from_float(2.15)
Decimal('2.149999999999999911182158029987476766109466552734375')
>>>Decimal.from_float(2.55)
Decimal('2.54999999999999982236431605997495353221893310546875')
尽量避免使用round()。