Python: Different result when performing division vs. floor division when dividing <0.1

和自甴很熟 提交于 2020-01-15 08:11:13

问题


Accidentally I stumbled upon the following difference. The division 50/0.02 returns a float:

2500.0

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?


回答1:


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.

Have a look at the python docs to have a better understanding about floating point numbers in python



来源:https://stackoverflow.com/questions/24057694/python-different-result-when-performing-division-vs-floor-division-when-dividi

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!