C# 模运算符

发布于 2024-09-13 09:50:55 字数 153 浏览 4 评论 0原文

我可以编写程序

int a = 3;
int b = 4;

Console.WriteLine(a % b);

我得到的答案是 3。 3 mod 4 = 3 怎么办?

我无法弄清楚这是如何以这种方式计算的。

I can write the program

int a = 3;
int b = 4;

Console.WriteLine(a % b);

The answer I get is 3. How does 3 mod 4 = 3???

I can't figure out how this is getting computed this way.

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

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

发布评论

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

评论(7

萧瑟寒风 2024-09-20 09:50:55

我不太确定会发生什么,
但我不明白如何
余数为 3。

因此,您有 3 块 cookie,您想将它们平均分配给 4 个人。

因为人数比 cookie 多,所以没有人获得 cookie( = 0),而您自己则拥有剩余 3 个 cookie。 :)

I wasn't quite sure what to expect,
but I couldn't figure out how the
remainder was 3.

So you have 3 cookies, and you want to divide them equally between 4 people.

Because there are more people than cookies, nobody gets a cookie (quotient = 0) and you've got a remainder of 3 cookies for yourself. :)

假扮的天使 2024-09-20 09:50:55

3 mod 4 是 3 除以 4 的余数

。在这种情况下,4 被 3 个零除以,余数为 3。

3 mod 4 is the remainder when 3 is divided by 4.

In this case, 4 goes into 3 zero times with a remainder of 3.

甲如呢乙后呢 2024-09-20 09:50:55

我发现接受的答案令人困惑和误导。

与返回被除数和除数之比的现代除法不同。这通常表示为小数商,其中包括商中的余数。这就是让人绊倒的原因。

模数只是除法中用于十进制商之前的余数

示例:两个数字相除通常表示为十进制数 (商)。但是除法的结果,例如1/3,也可以用整数表示为“0 余数为 1”。但这种形式的商在现代数学中并不是很有帮助,因此我们在现代计算器中经常看到返回的是十进制值或两个数字的比率

1 / 3 = .333333333......

但模数并不是这样工作的。它忽略除法返回的小数商值或比率,取1/3中“0余数为1”的商表达式,并提取1或< em>剩余部分从该部门返回。它只是从商中去掉余数,并在转换为小数之前吐出除法的余数。下面是模数的工作原理...

1 % 3 = 1

因此,描述模数的更好方法是说它是除第二个数字后第一个数字(被除数)中剩下的整数(余数)尽可能多次地将数字(除数)代入其中。

1 % 1 = 0 because after dividing 1 into 1, one time, there's nothing left
2 % 1 = 0 because after dividing 1 into 2, two times, there's nothing left
1 % 2 = 1 because 2 won't go into 1, so 1 is left

这些由(模数学)返回的整数余数在软件程序中非常有用,可用于提取一周中的某一天、创建交替序列、查找数字是偶数还是奇数, ETC。

I found the accepted answer confusing and misleading.

Modulus is NOT the same as modern division that returns a ratio of dividend and divisor. This is often expressed as a decimal quotient that includes the remainder in the quotient. That is what trips people up.

Modulus is just the remainder in division before its used in a decimal quotient.

Example: The division of two numbers is often expressed as a decimal number (quotient). But the result of the division of say, 1/3, can also be expressed in whole numbers as "0 with a remainder of 1". But that form of quotient is not very helpful in modern math, so a decimal value or the ratio of the two numbers is often what we see returned in modern calculators.

1 / 3 = .333333333......

But modulus does not work that way. It ignores the decimal quotient value or ratio returned from division, takes the quotient expression of "0 with a remainder of 1" in 1/3, and extracts the 1 or remainder that was returned from that division. It just strips out the remainder from the quotient and spits back the remainder of division before its converted to a decimal. Below is how modulus works...

1 % 3 = 1

As such, a better way of describing Modulus is to say it is what is left over as an integer (remainder) in the first number (dividend) after dividing the second number (divisor) into it as many times as possible.

1 % 1 = 0 because after dividing 1 into 1, one time, there's nothing left
2 % 1 = 0 because after dividing 1 into 2, two times, there's nothing left
1 % 2 = 1 because 2 won't go into 1, so 1 is left

These whole number remainders returned by modulus (modular math) are useful in software programs in extracting the day of a week, creating alternating sequences, finding if a number is even or odd, etc.

面如桃花 2024-09-20 09:50:55

我已经认为用户可能已经理解了答案。因为优秀的程序员太多了..用简单的语言 % 告诉你除以你自己的整数后的提醒。

例如

int a = int.Parse(Console.ReadLine());
int b = a % 2;

,现在您输入 13,它将给出 1,因为在简单数学中,13 除以 2 余数就是 1。希望你明白了。

I already think that the user may have understood the answers. Because there are so many good programmer.. in simple wording % tells you the reminder after dividing with your own integer.

e.g.

int a = int.Parse(Console.ReadLine());
int b = a % 2;

Now your input 13, it will give 1, because after diving 13 by 2 remainder is 1 in simple mathematics. Hope you got that.

冰雪之触 2024-09-20 09:50:55

正如其他人所解释的,但如果您不想使用“mod”运算符。这是计算“a”除以“n”的余数的方程式

a-(n* int(a/n))

As explained by others, but if you don't want to use the "mod" operator. Here is the equation to figure out the remainder of "a" divided by "n"

a-(n* int(a/n))

巴黎盛开的樱花 2024-09-20 09:50:55

另一个“正如其他人所解释的”,但如果您对其他几种求模的方法(或使用替代方法)感到好奇,您可以 阅读这篇文章,它对几种不同的方法进行了基准测试

基本上,最快的方法是老式的模数运算符,类似于:

if (x % threshold == some_value)
{
    //do whatever you need to
}

Another "as explained by others", but if you're curious about several more ways to do modulus (or use an alternative method), you can read this article which benchmarks a few different ways.

Basically, the fastest way is the good old fashioned modulus operator, similar to:

if (x % threshold == some_value)
{
    //do whatever you need to
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文