当第二个数字大于第一个数字时,为什么Mod运算符返回第一个数字?

时间:2015-04-16 04:00:54

标签: c# modulo

我希望我没有提出一个愚蠢的问题但是,我无法对此结果找到任何好的解释:

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

任何人都能解释一下吗?

3 个答案:

答案 0 :(得分:5)

  

当我们除以13%3时,它给出1(余数)

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

当你dividing 35/36, integer division will give you 0 quotient.时 浮点除法将给出分数值,分数值是剩余部分。

  

13/3 = 4.33333 = 4 * 3 +(0.333)* 3                   =(整数商) divider + remainder。

答案 1 :(得分:4)

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

答案 2 :(得分:0)

mod为您提供整数除法的提醒。 36适合35次,因此 35/36为0并且提醒35为mod

的结果
相关问题