问题
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