0.1+0.1+0.1-0.3=0 吗?

10/3
3.3333333333333335

0.1 + 0.1 + 0.1 - 0.3
5.551115123125783e-17

>>> 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,而事关浮点数存储:

>>> from decimal import Decimal
>>> 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()函数。

全部评论

相关推荐

挣K存W养DOG:入职送金条全球游,路过缅甸停一下🐔
点赞 评论 收藏
分享
点赞 收藏 评论
分享
牛客网
牛客企业服务