Python 为什么将-2//4的输出设置为-1?
我得到了Python 为什么将-2//4的输出设置为-1?,python,python-3.x,math,integer,floor,Python,Python 3.x,Math,Integer,Floor,我得到了-1作为-2//4的输出 print(-2//4) 在Python3中,/操作符生成结果的底部 Floor将使数字变为下一个较低的整数 您使用的是负数,因此-1小于0 -2 / 4 >> -0.5 math.floor(-0.5) >> -1 -2 // 4 >> -1 “根据python,输出应该是0。”“根据python”是什么意思?您为什么期望结果0?在Python3中,/操作符对结果调用math.floor()”——不,没有
-1
作为-2//4
的输出
print(-2//4)
在Python3中,
/
操作符生成结果的底部
Floor将使数字变为下一个较低的整数
您使用的是负数,因此-1
小于0
-2 / 4
>> -0.5
math.floor(-0.5)
>> -1
-2 // 4
>> -1
“根据python,输出应该是0。”“根据python”是什么意思?您为什么期望结果
0
?在Python3中,/
操作符对结果调用math.floor()
”——不,没有。它生成除法精确结果的下限,而不是对任何对象调用math.floor
。这是因为避免了浮点舍入。
-2 / 4
>> -0.5
math.floor(-0.5)
>> -1
-2 // 4
>> -1