Example: 340%60 = 40, how did it come from?
340 – 60*5 = 40
340 – (The positive integer smaller than 340 that is divisible by 60) =. 40
**If it is negative: **
As shown:
You can also think about it differently,
For example:
1 %3 = 1
Supplementary knowledge: the calculation method of calculating the remainder of negative numbers in python and the points of attention in exponentiation operation
The calculation method of calculating the remainder of negative numbers in python
1. Divide operation (after the result of division is calculated, round down)
Use the division operation //
1 // 203// 2110// 3310// -3-4
10 / 3 = 3.3333
Therefore: 10 // 3.33 is rounded down to 3 in 3
10 / -3 = -3.333
10 // -3 is rounded down to -3.33 to equal -4
*2. Find the remainder (use %) x% y is equivalent to x – (x // y)y
- x% y is equivalent to x – (x // y)y
10 %-3-2-10%32
10 How is% -3 = -2 calculated?
x% y is equivalent to x – (x // y)y
10 // -3 = -4 has been calculated above
10 % -3 = 10 – (-4)(-3) = 10 – 12 = -2
10 % 3 = -10 – (-4)*3 = -10 + 12 = 2
One point to note in exponentiation
**3. Use **** for exponentiation
-3**2-9(-3)**29
The precedence of the exponentiation operator is higher than the negation (-).
The above python negative modulus calculation example is all the content shared by the editor, I hope to give you a reference.
Recommended Posts