Modulo and remainder, modulo Remainder
For integer a and B, the modulo or remainder operations are all performed: 1. evaluate the integer quotient: c = a/B; 2. modulus or remainder: r = a-c * B. the modulo operation and the remainder operation are different in the first step: When the remainder operation is used to obtain the value of c, it rounds to 0 (the fix () function ); while the modulo operation calculates the value of c and rounds the value to the negative infinity (floor () function ). For example: Calculate-8% 3 so: a =-8; B = 3; Step 1: Calculate the integer quotient c, (float) c = a/B =-2.67, for example, perform the modulo operation c =-3 (round to the negative infinity direction) and perform the remainder c =-2 (round to the 0 direction). Step 2: the formula for calculating the modulus and the remainder is the same, but because of the different values of c, r = 1 for the modulus and r =-2 for the remainder. Induction: When the symbols a and B are consistent, the result is consistent because the modulo operation and the result of the remainder operation are the same. 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 each environment, the % operator has different meanings. For example, c/c ++, java is the remainder operator, and python is the modulo operator. In terms of mathematics, both operators are correct. Many languages simply propose two functions, namely the remainder rem and mod. In fact, a mod B = floor (float) c) = floor (a/B) = floor (-2.67) =-3; a rem B = a mod B + 1; floor indicates downgrading. Corresponding, rem can also be implemented with a fix.