当第二个数字大于第一个时,为什么 Mod operator returns 第一个数字?

Why does Mod operator returns the first number when the second number is larger than the first?

我希望我不是在问一个愚蠢的问题,但是,我找不到关于这个结果的任何好的解释:

35 % 36 等于 35

https://www.google.com.ph/webhp?sourceid=chrome-instant&ion=1&espv=2&ie=UTF-8#q=35%20%25%2036

但是如果我将这两个数字相除,35 / 36 结果是:0.97222222222 我假设余数是 97.

谁能解释一下?

mod 提醒您整数除法。 36 在 35 中出现 0 次所以 35 / 36 是 0 并且提示 35 是 mod

的结果

35 % 36的结果相当于35除以36,返回余数。由于 36 恰好进入 35 0 次,所以你剩下 35 的余数。

同样,假设您这样做 7 % 3。在此示例中,3 进入 7 两次,余数为 1。因此 7 % 3 == 1.


我没有该操作的源代码,但您可以使用像这样的小函数来模仿它(我敢肯定这不如内置的任何东西有效!):

public static class MyMath
{
    public static int Mod(this int operand1, int operand2)
    {
        while (operand1 >= operand2)
            operand1 -= operand2;

        return operand1;
    }
}

然后这样称呼它:

var remainder1 = 7.Mod(3);    // 1
var remainder2 = 35.Mod(36);  // 35

When we divide 13 % 3 it gives 1 (the remainder)

类似地,当我们做 35 % 36 时,它会给出第一个数字作为余数,因为红利小于除数。

当你 dividing 35/36, integer division will give you 0 quotient. 浮点数除法会给你分数值,分数值就是余数。

13/3 = 4.33333 = 4 * 3 + (0.333)* 3 =(integer quotient) divider + remainder.