python - Python 中的整数除法

标签 python math integer python-2.x integer-division

我对 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/

相关文章:

python - Lambda 不适用于过滤器

python - 当我增加神经元或层数时,我的 tensorflow 模型无法训练

c++ - 如何将数组从c++传递到python函数并将python返回的数组检索到c++

javascript - Math.Random 无法正常工作 typescript

algorithm - n 的四次方根的复杂度函数的大 O 表示法

python - 如何在内存中连续排列现有的Python整数列表?

c++ - 整数到字符串优化函数?

c - AVX2 64位无符号整数比较

python - 将多索引 DataFrame 的所有列乘以 Series 中的适当值

arrays - 简单的面试问题变得更难了 : given numbers 1. .100,找到恰好 k 丢失的缺失数字