I always thought that modulo and remainder are the same thing. I found that the two are different. The following documents are reprinted on the Internet:
In general, MOD and REM operations are confused, because in most programming languages, the '%' symbol is used to represent a modulo or remainder operation. Note the specific meaning of the '%' operator in the current environment, because the results of the two operators are different when a negative number exists.
For integer a and B, the modulo operation or remainder operation methods are as follows:
1. Calculate the integer quotient: c = A/B;
2. computing model or number: r = a-c * B.
The modulo operation and the remainder operation are different in the first step: When the modulo operation is used to obtain the value of C, it rounds to 0 (the fix () function ); when the remainder operation calculates the value of C, it rounds the floor () function to an infinitely small direction ).
Therefore, when the symbols A and B are the same, the result is the same because the modulo operation and the result of the remainder operation are the same. But when the symbols are inconsistent, the results are different. The symbols of the result of the modulus operation are the same as those of B, and the symbols of the result of the remainder operation are the same as those of.
In C, the % symbol represents the remainder operation. In a Python script, % Represents the modulo. (In the modulo operation, B is not allowed to be a negative number, but in Python 2.5.1, it can be followed by a negative number after %, because in Python, the division result is rounded to 0, therefore, the calculation result is a modulo !)
The following table lists some typical modulo or remainder values.
A
|
B
|
C language: A % B (remainder)
|
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
|
Modulus and remainder