Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 为什么将-2//4的输出设置为-1?_Python_Python 3.x_Math_Integer_Floor - Fatal编程技术网

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