% is a remainder operator, also known as modular division operator, used to find the remainder. % requires both operands to be integers (or types that can be implicitly converted to integers).
The standard stipulates that:
1. If the operand to the left of% is negative, the result of modulo division is negative or 0.
2. If the operand to the left of% is positive, the structure of modulo division is positive or 0.
Test code:
Implementation results:
Extended data:
With regard to remainder, positive integer p and integers a and b, the following operations are defined:
1, modular operation: a% p (or a mod p), indicating the remainder of a divided by p.
2. modular p addition: the result is the remainder of a+b arithmetic sum divided by p.
3. modular p subtraction: the result is the remainder of a-b arithmetic difference divided by p.
4. modular p multiplication: the result is that the remainder of a * b arithmetic multiplication is divided by p.
Description:
1, congruence formula: positive integer a and b module p, and their remainders are the same, which is recorded as or a ≡ b (mod p).
2. The positive and negative results of n% p are determined by the dividend n, which has nothing to do with p ... For example, 7%4 = 3, -7%4 = -3, 7%-4 = 3, -7%-4 = -3.
References:
Baidu encyclopedia-modular operation