Integer division compared to floored quotient: why this surprising result?

前提是你 提交于 2019-12-30 06:14:14

问题


The // "integer division" operator of Python surprised me, today:

>>> math.floor(11/1.1)
10.0
>>> 11//1.1
9.0

The documentation reads "(floored) quotient of x and y". So, why is math.floor(11/1.1) equal to 10, but 11//1.1 equal to 9?


回答1:


Because 1.1 can't be represented in binary form exactly; the approximation is a littler higher than 1.1 - therefore the division result is a bit too small.

Try the following:

Under Python 2, type at the console:

>>> 1.1
1.1000000000000001

In Python 3.1, the console will display 1.1, but internally, it's still the same number.

But:

>>> 11/1.1
10.0

As gnibbler points out, this is the result of "internal rounding" within the available precision limits of floats. And as The MYYN points out in his comment, // uses a different algorithm to calculate the floor division result than math.floor() in order to preserve a == (a//b)*b + a%b as well as possible.

Use the Decimal type if you need this precision.



来源:https://stackoverflow.com/questions/2019588/integer-division-compared-to-floored-quotient-why-this-surprising-result

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