我最近注意到,int()
轮朝着0的方向旋转,而integer division轮朝着地板的方向旋转.
例如:
-7 // 2 == -4
int(-7/2) == -3
我已经阅读了文件,其中规定:
类int(x,基数=10)
返回由数字或字符串x构造的整数对象,如果没有参数,则返回0>;鉴于如果x是一个数字,返回
x.__int__()
.对于浮点数,this truncates towards zero.
以及:
楼层划分
Mathematical division that rounds down to nearest integer. The 楼层划分 operator is
//
. For example, the expression11 // 4
evaluates to 2 in contrast to the2.75
returned by float true division. Note that(-11) // 4
is-3
because that is-2.75
rounded downward. See PEP 238.
但对我来说,两个类似的操作(浮点除法到整数)应该返回不同的结果似乎不合逻辑.
功能之间的差异有什么动机吗?