What Is The Purpose of Negative Modulus Operator Results?

穿精又带淫゛_ 提交于 2019-12-11 02:46:51

问题


I was previously under the (naive) assumption that the modulus operator returned the remainder of division. I was apparently wrong, as -2 % 5 returns 3. I would have thought that 5 divides -2 zero times with -2 as the remainder.

Now I understand the mechanics of how this operation is performed, but my question is why? Could someone give me a link to something that explains why modulus and remainder are not synonymous, or an example of a situation where it would be useful?


回答1:


The result is entirely correct. Modular arithmetic defines the following (I'll use "congruent" since I can't type the equal sign with three lines)

a congruent b mod c iff a-b is a multiple of c, i.e. x * c = (a-b) for some integer x.

E.g.

0 congruent 0 mod 5 (0 * 5 = 0-0)
1 congruent 1 mod 5 (0 * 5 = 1-1)
2 congruent 2 mod 5 (0 * 5 = 2-2)
3 congruent 3 mod 5 (0 * 5 = 3-3)
4 congruent 4 mod 5 (0 * 5 = 4-4)
5 congruent 0 mod 5 (1 * 5 = 5-0)
6 congruent 1 mod 5 (1 * 5 = 6-1)
...

The same can be extended to negative integers:

-1 congruent 4 mod 5 (-1 * 5 = -1-4)
-2 congruent 3 mod 5 (-1 * 5 = -2-3)
-3 congruent 2 mod 5 (-1 * 5 = -3-2)
-4 congruent 1 mod 5 (-1 * 5 = -4-1)
-5 congruent 5 mod 5 (-1 * 5 = -5-0)
-6 congruent 4 mod 5 (-2 * 5 = -6-4)
-7 congruent 3 mod 5 (-2 * 5 = -7-3)
...

As you can see, a lot of integers are congruent 3 mod 5: ..., -12, -7, -2, 3, 8, 13, ...

In mathematics, the set of these numbers is called the equivalence class induced by the equivalence relation "congruence". Our understanding of the remainder and the definition of the "mod" function are based on this equivalence class. The "remainder" or the result of a mod computation is a representative element of the equivalence class. By declaration we have chosen the smallest non-negative element (so -2 is not a valid candidate).

So when you read -2 mod 5 = x this translates to "Find the smallest non-negative x so that there exists an integer y with y * 5 = -2 - x", in concordance with the definition of congruence. The solution is y=1 and x = 3 as you can see by simply trying out other values for y.




回答2:


a = n (mod m) is defined as a = n + m*t and it applies to negative numbers equally well. (Another to look at it is that a = n (mod m) means (a - n) is a multiple of m)

-2 = 3 (mod 5) because -2 = 3 - 5 (i.e. t = -1)

The convention is that the result of taking a modulo m is a number between 0 and m - 1 (inclusive)




回答3:


The fundamental guarantee that you get is that

(a % b) + b * (a / b)  == a

For signed values, there is no reason either sign should be the preferred outcome of a modulo or divide operation. Some languages fix one form, others leave it up to the implementation, so that the implementation can use whichever way the hardware happens to provide. The hardware instruction, in turn, may have been chosen to operate efficiently the hardware's representation of signed integers.

Generally, be very careful when using signed integers together with division, remainder and bit shift operations.




回答4:


I guess it depends on whether you want your result rounded down or rounded towards 0:

2 / 5 = 0.4 = 5*0 + 2 works in both cases, whereas
-2 / 5 = -0.4 = 5*0 + -2 if you're rounding towards 0 (truncation),
-2 / 5 = -0.4 = 5*-1 + 3 if you're rounding down (floor).

Note that the result is always positive (for a positive divisor) in the second case and it would be useful, for example, when calculating an array index:

hashmapBuckets[getIntHash(obj) % hashmapBuckets.size].add(obj)

or normalizing an angle:

angle = angle % 360; //0-359

It is in fact the other case I'm having trouble finding practical examples for :)

--

Oh, and the Wikipedia page on the modulo operation has some nice graphs. Note that the remainder always has the same sign as the divisor for a floored division.




回答5:


Think of modulo as an operator that wraps a line of length y (in terms of y % x) around a circle of x pegs. The remaining length of the line that doesn't fully wrap around x is the resultant.



来源:https://stackoverflow.com/questions/6755946/what-is-the-purpose-of-negative-modulus-operator-results

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!