Location>code7788 >text

Analyzing the rules for taking modulus and remainder of negative numbers

Popularity:877 ℃/2024-09-19 11:34:37

catalogs
  • Negative "modulo"
    • basic concept
    • Definition of amendment
    • The rounding rule determines the value of the quotient
    • Taking a mode is not the same as taking a remainder.

Negative "modulo"


basic concept

If a and d are two natural numbers and d is nonzero, it can be shown that there exist two unique integers q and r satisfying a = q*d + r,and 0 <= r < d. Where q is called the quotient and r is called the remainder.

//corresponding code

int main()
{
int a = 10;
int d = 3;
printf("%d\n", a % d); //the result is 1
//because: a=10,d=3,q=3,r=1 0<=r<d(3)
// So: a = q*d+r -> 10=3'3+1

return 0;

}

Positive numbers are relatively simple, we do not talk about, just look at the negative numbers, the negative number of the situation is still more complex, let's look at the different platforms under the negative number of "modulus" differences

int main()
{
    int a = -10; 
    int d = 3;
    printf("%d\n", a/d); 
    printf("%d\n", a%d);
}

image-20240504114217545

You can see that there is no difference between the C "modulo" and "quotient" on different platforms. Let's take a look at python.

centos7 default python version is 2.7.5, so use python 2.7.5 version test. You can also use python version 3.7.3 to test, the result is the same!

image-20240914175700078

It can be noticed that the results of quotienting and balancing negative numbers are different in the two languages.

The definition states that the remainder is to be greater than or equal to 0, but C calculates it as a negative number.

reach a verdict: Clearly, the above definition of modulo does not satisfy the linguistic modulo operation.

Thus leading to theDefinition of amendment


Definition of amendment

Because in C, -10%3 appears negative, by definition: satisfies a = q*d + r and 0 <= r < d. The remainder, in C, does not satisfy the definition, because, r<0 now.
Therefore, there is a revised version of the definition of taking a die:
If a and d are two natural numbers and d is nonzero, it can be shown that there exist two unique integers q and r satisfying a = q*d + r , q is an integer, and 0 <= |r| < |d|. where q is called the quotient and r is called the remainder.
With this new definition, "modulo" in C or Python can be explained.
Explanation C: -10 = (-3) ' 3 + (-1)
Explain Python: -10 = (?)' 3 + 2, where it can be deduced that, '?' must be -4 (why, later), i.e. -10 = (-4)' 3 + 2, in order to satisfy the definition.
So, in different languages, the result of "taking the modulus" of a negative number is different for the same expression. We can call them positive and negative remainders, respectively.


What determines this phenomenon?

The size of the specific remainder r depends essentially on the quotient, and once the quotient is determined, the remainder can be determined.

And what does the quotient depend on? The answer is, it depends on the result of the division.Rounding rules.


The rounding rule determines the value of the quotient

The default rounding rule in C is to round up to 0, and the default rounding rule in python is to round down.

According to the rules for both of them, it is clear that the cases greater than 0 are the same, while the cases less than 0 are different, so they are not the same in the case of negative numbers.

Because they have different rules for rounding, and must satisfy the revised version of the "modulo" rule, which determines the phenomenon of their calculation results.

One more question, is it still modulus or remainder since the % sign calculates different values in different languages? Or is taking a remainder the same as taking a modulus?


Taking a mode is not the same as taking a remainder.


define

Remainder: Let the quotient, as far as possible, be rounded to zero. // i.e. zero-ward rounding yields the remainder.

Modulo: let the quotient, as far as possible, be rounded down towards -∞. // i.e., the downward rounding approach yields the modulus
//Modulus, in terms of geometric vectors, modulus is length, positive. (Complex numbers are not considered). Whether a modulus is positive or negative depends on the definition of the field in question. In computer science, modulus and remainder are not distinguished in many cases.

By definition.

The % operation in C is essentially a remainder.

The % operation in python is essentially modulo.

Again, according to the rounding rule.

image-20240504125433531

For any number greater than 0, the direction of rounding to 0 and -∞ is the same. Therefore, taking the modulus is equivalent to taking the remainder
For any number less than 0, it is rounded to 0 and rounded to -∞, and the directions of rounding are reversed. Therefore, taking the modulus is not equivalent to taking the remainder


Subtotal.

Dividing data with the same sign, the resulting quotient, according to the mathematical rule of elimination with the same sign, must be a positive number, that is, greater than 0! Greater than 0 is the same way of rounding.
Therefore, when rounding its quotient, taking the modulus is equivalent to taking the remainder.