简体   繁体   English

请帮助使模数简单易懂

[英]Please help make Modulo Simple To Understand

(Sorry in advance if this has been asked before, I searched and couldn't find a similar question) (如果之前有人问过这个问题,请提前抱歉,我搜索并找不到类似的问题)

So I believe modulo (%) gives me the remainder of a long division equation.所以我相信模 (%) 给了我一个长除法方程的余数。 so 2%4 =0r.所以 2%4 =0r。 So in simple terms a modulo equation that equals zero should be an even number.所以简单来说,一个等于零的模方程应该是一个偶数。 and a modulo equation that equals 1, should be an odd number?而一个等于 1 的模方程应该是一个奇数? Is that correct?那是对的吗?

Here's where I begin to confuse myself.这就是我开始混淆自己的地方。

What about equations that equal an even or odd remainder, would that still output an equal or odd number.等于偶数或奇数余数的方程如何,仍然会输出相等或奇数。

For instance.例如。 5%149 equals 4r.. the remainder is an even number, so is the output all even numbers.. or does the very fact that there is any remainder at all mean that the output will be odd numbers?? 5%149 等于 4r .. 余数是偶数,所以输出都是偶数.. 或者有任何余数的事实是否意味着输出将是奇数?

TLDR, is modulo as simple as 0r outputs even numbers. TLDR 是模数,就像 0r 输出偶数一样简单。 And anything with 1 or more remainder outputs odd numbers.任何有 1 个或多个余数的东西都会输出奇数。

a modulo equation that equals zero should be an even number.等于零的模方程应该是偶数。 and a modulo equation that equals 1, should be an odd number?而一个等于 1 的模方程应该是一个奇数?

You've probably seen modulo as a way of testing for evenness but this is not right.您可能已经将模数视为测试均匀性的一种方式,但这是不对的。 It should read它应该读

a modulo by 2 operation that equals zero is an even number等于 0 的模乘 2 运算是偶数

ie. IE。 x % 2 == 0 implies x is even. x % 2 == 0意味着 x 是偶数。 Because x is divisible by 2. x % 3 == 0 means x is divisible by 3.因为 x 可以被 2 整除。 x % 3 == 0意味着 x 可以被 3 整除。

Here's one way I learned to look at it.这是我学会看待它的一种方式。 Consider an analogue clock with n hours on in (perhaps n=12 or n=24 or some other funny clock).考虑一个有 n 小时的模拟时钟(可能 n=12 或 n=24 或其他一些有趣的时钟)。 The first number in the modulo operation tells you how many hours forward to traverse, going round and round the circle.模运算中的第一个数字告诉您向前遍历、绕圈走多少小时。 The second number (n) tells you how many hours is built into the clock.第二个数字 (n) 告诉您时钟内置了多少小时。

Here are some examples:这里有些例子:

You go forward 5 on a 12 hour clock and land on 5 o'clock.您按 12 小时制前进 5 点,然后在 5 点钟着陆。

5 % 12 == 5

You go forward 13, completing one full loop plus one more hour, landing on 1 o'clock.你前进 13,完成一个完整的循环,再加上一个小时,在 1 点钟着陆。

13 % 12 == 1

You go forward 24, completing 2 full loops but landing at the starting point, 0. (ok most clocks have 12 at the top but its the same as 0.)你前进 24,完成 2 个完整的循环,但降落在起点 0。(大多数时钟在顶部有 12,但与 0 相同。)

24 % 12 == 0

Consider a clock or spinning wheel with 4 categories.考虑一个有 4 个类别的时钟或纺车。

Start at the base and take 7 steps forward.从基地开始,向前走 7 步。 That give you one full traversal (4 steps) then 3 more steps lands you on the 3rd item.这会给你一个完整的遍历(4 步),然后 3 步让你到达第 3 项。

7 % 4 == 3

You've just stepped forward 2. Because the wheel has 4 slots, the counting hasn't reset yet.您刚刚向前迈出了 2。因为轮子有 4 个槽位,所以计数还没有重置。

2 % 4 == 2

So to recap, the first number is the number of steps to take, the second number is the size of the clock.总结一下,第一个数字是要走的步数,第二个数字是时钟的大小。

Modulo (or modulus) is used to see the remainder of a division.模数(或模数)用于查看除法的余数。

You can flip it on it's head and use multiplication to help you out as well if necessary.您可以将其翻转过来,并在必要时使用乘法来帮助您。 I've provided some examples.我已经提供了一些例子。

Try doing something like this:尝试做这样的事情:

From the equation you posted in your example: 149 % 5 would give you the remainder of 4. Reason for this: The last multiple of 5 you can get before 149 is 145, and your modulus equation is telling you that you have 4 left over.根据您在示例中发布的等式: 149 % 5将为您提供 4 的余数。 原因:在 149 之前您可以得到的最后一个 5 倍数是 145,而您的模数方程告诉您您还剩下4 .

Now, if you were to do something like 150 % 5, your remainder would be 0 because 150 is a safe multiple of 5.现在,如果您要执行 150 % 5 之类的操作,那么您的余数将为0因为150是 5 的安全倍数。

Some documentation should hopefully help you understand this a bit better as well: https://docs.onux.com/en-US/Developers/JavaScript-PP/Language/Reference/Expressions/arithmetic-operators/modulus一些文档也有望帮助您更好地理解这一点: https : //docs.onux.com/en-US/Developers/JavaScript-PP/Language/Reference/Expressions/arithmetic-operators/modulus

Some examples to help you understand the remainder: 10 % 5 = 0 Since 5 x 2 = 10 9 % 3 = 0 Since 9 x 3 = 9 6 % 2 = 0 Since 2 x 3 = 6 7 % 2 = 1 Since you can only multiply 2 three times to get 6, you are left with a remainder of 1.一些帮助您理解余数的示例: 10 % 5 = 0自 5 x 2 = 10 9 % 3 = 0自 9 x 3 = 9 6 % 2 = 0自 2 x 3 = 6 7 % 2 = 1因为您可以只需将 2 乘以 3 次即可得到 6,余数为 1。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM