C# % (mod) 解释

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10065080/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 12:19:14  来源:igfitidea点击:

% (mod) explanation

c#mathoperatorsmodulo

提问by Wolfy

Today I was writing a program in C#, and I used %to calculate some index... My program didn't work, so I debugged it and I realized that "%" is not working like in other program languages that I know.

今天我正在用 C# 编写一个程序,我用%来计算一些索引......我的程序不起作用,所以我调试了它,我意识到“ %”不像我知道的其他程序语言那样工作。

For example:

例如:

In Python %returns values like this:

在 Python 中%返回这样的值:

for x in xrange (-5, 6):
     print x, "% 5 =", x % 5

-5 % 5 = 0
-4 % 5 = 1
-3 % 5 = 2
-2 % 5 = 3
-1 % 5 = 4
0 % 5 = 0
1 % 5 = 1
2 % 5 = 2
3 % 5 = 3
4 % 5 = 4
5 % 5 = 0

In C#:

在 C# 中:

for (int i = -5; i < 6; i++)
{
    Console.WriteLine(i + " % 5 = " + i % 5);
}

-5 % 5 = 0
-4 % 5 = -4
-3 % 5 = -3
-2 % 5 = -2
-1 % 5 = -1
0 % 5 = 0
1 % 5 = 1
2 % 5 = 2
3 % 5 = 3
4 % 5 = 4
5 % 5 = 0

Did I do something wrong or is %not working like it should?

我做错了什么还是%没有像它应该的那样工作?

采纳答案by David Heffernan

As explained in the comments, the different behaviour is by design. The different languages just ascribe different meanings to the %operator.

正如评论中所解释的,不同的行为是设计使然。不同的语言只是赋予%运算符不同的含义。

You ask:

你问:

How can I use modulus operator in C#?

如何在 C# 中使用模数运算符?

You can define a modulus operator yourself that behaves the same way as the Python %operator:

您可以自己定义一个模运算符,其行为方式与 Python%运算符相同:

int mod(int a, int n)
{
    int result = a % n;
    if ((result<0 && n>0) || (result>0 && n<0)) {
        result += n;
    }
    return result;
}

回答by Hauleth

回答by Niet the Dark Absol

Both answers are correct. Although personally I think the "always positive" one makes more sense.

两个答案都是正确的。虽然我个人认为“总是积极的”更有意义。

You can define your own modulus function that only gives positive answers like this:

您可以定义自己的模函数,它只给出如下肯定的答案:

int mod(int a, int n) {
    return ((a%n)+n) % n;
}

回答by Sklivvz

In modular arithmetic, one defines classesof numbers based on the modulo. In other words, in modulo marithmetic, a number nis equivalent (read: the same) to n+ m, n- m, n+ 2m, n- 2m, etc.

模运算,一个定义的类号的基于模。换句话说,在模m算术中,数字n等价于(读作:相同)到n+ mn- mn+ 2mn- 2m等。

One defines m"baskets" and every number falls in one (and only one) of them.

一个定义了m 个“篮子”,每个数字都落在其中一个(并且只有一个)中。

Example: one can say "It's 4:30 pm" or one can say "It's 16:30". Both forms mean exactly the same time, but are different representationsof it.

例如:可以说“现在是下午 4:30”或可以说“现在是 16:30”。两种形式都意味着完全相同的时间,但对时间的表示不同。

Thus both, the Python and C# results are correct! The numbers are the samein the modulo 5arithmetic you chose. It would also have been mathematicallycorrect to return (5, 6, 7, 8, 9) for example. Just a bit odd.

因此,Python 和 C# 结果都是正确的!您选择的模5算术中的数字相同。例如,返回 (5, 6, 7, 8, 9)在数学上也是正确的。只是有点奇怪。

As for the choice of representation (in other words, the choice on how to represent negative numbers), that is just a case of different design choices between the two languages.

至于表示的选择(换言之,如何表示负数的选择),这只是两种语言之间设计选择不同的情况。

However, that is not at all what the % operator actually does in C#. The % operator is not the canonical modulus operator; it is the remainder operator. The A % B operator actually answer the question "If I divided A by B using integer arithmetic, what would the remainder be?"

但是,这完全不是 % 运算符在 C# 中实际执行的操作。% 运算符不是规范模数运算符;它是余数运算符。A % B 运算符实际上回答了“如果我使用整数算法将 A 除以 B,余数是多少?”的问题。

What's the difference? Remainder vs Modulusby Eric Lippert

——有什么区别?Eric Lippert 的余数与模数



Quick snippet to get the canonical modulus:

获取规范模数的快速片段:

return ((n % m) + m) % m;


Test implementation:

测试实现:

Mono/C#:

单声道/C#

machine:~ user$ cat mod.cs
using System;

public class Program
{
    public static void Main (string[] args)
    {
        Console.WriteLine(Mod(-2, 5));
        Console.WriteLine(Mod(-5, 5));
        Console.WriteLine(Mod(-2, -5));
    }

    public static int Mod (int n, int m)
    {
        return ((n % m) + m) % m;
    }
}

machine:~ user$ mono mod.exe
3
0
-2

Python:

蟒蛇

machine:~ user$ cat mod.py
print -2%5;
print -5%5;
print -2%-5;

machine:~ user$ python mod.py
3
0
-2