Always thought that the mold and the remainder is one thing, found that the two are different. The following information is reproduced on the Internet:
In general, modulo operations (mods) and Redundancy (REM) operations are confused because in most programming languages, the '% ' notation is used to denote modulo or remainder operations. Here to remind you to pay great attention to the specific meaning of the '% ' operator in the current environment, because in the case of a negative existence, the results are different.
For the integer number, a, the method of modulo operation or redundancy operation is:
1. Seek integer quotient: c = A/b;
2. Calculate the modulus or number: R = a-c*b.
The modulo operation and the remainder operation are different in the first step: when the modulo operation takes the value of C, it rounds to the 0 direction (fix () function), while the remainder operation calculates the value of C and rounds (the floor () function) to the infinity direction.
Therefore, when the A and B symbols are consistent, the value of the C values obtained by the modulo operation and the remainder operation is consistent, and the result is consistent. But when the symbols are inconsistent, the results are different. The symbol of the result of the modulo operation is consistent with B, and the sign of the result of the remainder operation is consistent with a.
In the C language, the% symbol represents the remainder operation, and in the Python script,% represents the modulo. (Generally, B is not allowed to be negative in the modulo operation, but in Python 2.5.1 can be followed by a negative number in%, because the result of division in the Python language is rounded to 0, so the result is modulo!) )
The following table is a typical modulus or redundancy value.
A |
B |
C Language: A%b (for redundancy) |
Python shell:a%b (modulo) |
-3 |
-5 |
-3 |
-3 |
-3 |
4 |
-3 |
1 |
-3 |
2 |
-1 |
1 |
-1 |
6 |
-1 |
5 |
-4 |
-3 |
-1 |
-1 |
2 |
4 |
2 |
2 |
5 |
3 |
2 |
2 |
4 |
-7 |
4 |
-3 |
4 |
-3 |
1 |
-2 |
-6 |
-5 |
-1 |
-1 |
(turn) to die and to seek redundancy