Python:除法时,除法小于0.1时,执行除法与落地除法的结果不同 [英] Python: Different result when performing division vs. floor division when dividing <0.1
问题描述
我偶然发现了以下区别。除法50 / 0.02返回浮点数:
Accidentally I stumbled upon the following difference. The division 50/0.02 returns a float:
2500.0
但是下限分割50 // 0.02返回-在我看来-错误的答案:
However a floor division 50//0.02 returns - as it seems to me - a wrong answer:
2499.0
有人可以解释造成这种差异的原因吗?
Can anybody explain how this difference is caused?
推荐答案
这是由于python的浮点精度问题。 0.02
实际上就是Martijn Pieters建议的 0.02000000000000000041633363423443370265886187553405762
,因此,如果将其除以50,则得出的值类似于 2499.99999999999994795875
,并且使用下限分割,该值被下限并变为 2499
。
This is due to python's floating point precision problems. 0.02
is actually as Martijn Pieters suggested 0.02000000000000000041633363423443370265886187553405762
and so this when divided by 50 gives a value like 2499.99999999999994795875
, and with floor division, this value is floored and becomes 2499
.
看看 python文档以更好地了解python中的浮点数
Have a look at the python docs to have a better understanding about floating point numbers in python
这篇关于Python:除法时,除法小于0.1时,执行除法与落地除法的结果不同的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!