模数如何工作以及为什么它在 Python 中与大多数语言不同?

发布于 2025-01-06 16:43:46 字数 473 浏览 4 评论 0原文

下面是一些 C++ 代码。如果你在 python 中尝试类似 -2%5 的结果,结果是正 3,而许多其他语言,如 c++ C# (代码) 和 flash 给出 -2

为什么他们给出 -2 并且一个版本比另一个版本更正确?

#include <cstdio>
int main(){
printf("%d\n", 2%5);
printf("%d\n", -2%5);
printf("%d\n", -2%77);
printf("%d\n", 2%-77);
printf("%d\n", -2%-77);
}

输出:

2
-2
-2
2
-2

Below is some code in C++. If you try something like -2%5 in python the result is positive 3 while many other languages like c++ C# (code) and flash give -2

Why do they give -2 and is one version more correct than the other?

#include <cstdio>
int main(){
printf("%d\n", 2%5);
printf("%d\n", -2%5);
printf("%d\n", -2%77);
printf("%d\n", 2%-77);
printf("%d\n", -2%-77);
}

Output:

2
-2
-2
2
-2

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

蓝海 2025-01-13 16:43:46

如果r = a % n,则对于某些qa = n * q + r。这意味着您对 r 的值有多种选择,具体取决于所选择的 q 的值。

我建议阅读 http://en.wikipedia.org/wiki/Modulo_operation,其中写着大多数编程语言选择 r-n r < n。这意味着,除非 r 为零,否则 r 的值有两种选择 - 一种是正数,一种是负数。不同的编程语言对于采用正数还是负数做出不同的决定。您会在该页面上找到一个表格,总结了不同语言的功能:

  • Python 选择与 n 具有相同符号的 r(这就是您在上面看到的)。
  • C++ 2011 选择与 a 符号相同的 r(在 2011 标准之前,它是实现定义的)。

如果你想确保在 Python 中得到正数,请使用以下命令:

r = a % n
if r < 0:
  r += n

If r = a % n, then a = n * q + r for some q. That means that you have many choices for the value of r, depending on the value of q that gets chosen.

I'd recommend reading http://en.wikipedia.org/wiki/Modulo_operation, which says that most programming languages choose r with -n < r < n. That means that, unless r is zero, you have two choices for the value of r - one positive, one negative. Different programming languages make different decisions about whether to take the positive or negative one. You'll find a table on that page that summarizes what different languages do:

  • Python chooses r with the same sign as n (which is what you see above).
  • C++ 2011 chooses r with the same sign as a (and before the 2011 standard, it's implementation defined).

If you want to be sure that you get the positive one in Python, use this:

r = a % n
if r < 0:
  r += n
恋竹姑娘 2025-01-13 16:43:46

根据 C++ 文档

对于负值,结果可能会因库实现而异。

这看起来很奇怪。 Python 文档仅说明了这一点:

模运算符总是产生与其第二个操作数具有相同符号(或零)的结果;结果的绝对值严格小于第二个操作数的绝对值。

在我看来,Python 方式更符合逻辑,但这只是一种直觉。

According to the C++ documentation:

For negative values, the result may vary depending on the library implementation.

Which seems odd. The Python documentation says only this:

The modulo operator always yields a result with the same sign as its second operand (or zero); the absolute value of the result is strictly smaller than the absolute value of the second operand.

It seems to me that the Python way is more logical, but that's just a gut feeling.

纸短情长 2025-01-13 16:43:46

我想你应该看看下面的内容。除了使用稍微不同的算法之外,运算符优先级也很重要。尝试使用括号:

In [170]: 2%5
Out[170]: 2

In [171]: -2%5
Out[171]: 3

In [172]: (-2)%5
Out[172]: 3

In [173]: -(2%5)
Out[173]: -2

I think you should look at the below. In addition to using slightly different algorithms, operator precedence matters. Try it with brackets:

In [170]: 2%5
Out[170]: 2

In [171]: -2%5
Out[171]: 3

In [172]: (-2)%5
Out[172]: 3

In [173]: -(2%5)
Out[173]: -2
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文