Python3: 为什么-1//10 = -1?

2024-05-23 15:47:29 发布

您现在位置:Python中文网/ 问答频道 /正文

1//10==0,带整数除法,所以-1//10!= 0 ? 你知道吗

MPython 3.7.0b3 (v3.7.0b3:4e7efa9c6f, Mar 29 2018, 18:42:04) [MSC v.1913 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> -1 // 10
-1

这应该发生吗?你知道吗


Tags: orforlicenseontypebithelp整数
2条回答

这样解释比较容易:

运算//舍入“向左”整数,即

1//10 -> 0.1 -> 0
-1 //10 -> -0.1 -> -1 (as -1 on the X axis is to the left of -0.1)

楼层划分总是四舍五入。你知道吗

1 / 10 = .1 

四舍五入为0。你知道吗

-1 / 10 = -.1 

四舍五入到-1。你知道吗

相关问题 更多 >