我对 python 中的以下整数数学感到困惑:
-7/3 = -3
自 (-3)*3 = -9 < -7
.我明白了。
7/-3 = -3
我不明白这是怎么定义的。 (-3)*(-3) = 9 > 7
.在我看来,它应该是-2,因为(-3)*(-2) = 6 < 7
.
这是如何工作的?
最佳答案
来自documentation :
For (plain or long) integer division, the result is an integer. The result is always rounded towards minus infinity: 1/2 is 0, (-1)/2 is -1, 1/(-2) is -1, and (-1)/(-2) is 0.
向 -inf
的舍入解释了您所看到的行为。
关于python - Python 中的整数除法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7904445/