Circle problem:
For integer operations, one important thing to understand is that it rounds down the result, that is, the value after rounding is not greater than the actual value. Therefore, if the result is negative, the rounded result will be farther from zero, such as – 3.3 rounded result is – 4. This means that the -10 / / 3 will be rounded down to the top.-4, instead of rounding up to -3.
>>> 10 // 3
3
>>> 10 // -3 The 10 / / 3 rounding in the left code is 3; -10 / 3.
-4 The actual value should be -3.33333, so circle down to -4.
>>> -10 // 3
-4
>>> -10 // -3 -10 // -3The value should be 3.33333, down to 3.
3
The integer division (//) is always rounded downward, while the built-in function round rounds rounded to the nearest integer and rounded to even when two integers are close together.
>>> round(3.2)
3
>>> round(3.6)
4
>>> round(3.5)
4
>>> round(2.5)
2
Module selection:
>>> 10 % 3
1
>>> 10 % -3 The 10% code for the left code is 3; the -10 / / 3 is 1.
-2 The actual value should be -3.33333, down to -4.
>>> -10 % 3 And -4 * 3 = -12, so -10% 3 = 2 (-4 * 3 + 2 = 10);
2 10 % -3 = -2(-4 * -3 -2 =10);
>>> -10 % -3 -10 % -3 = -1(3 * -3 + -1 =-10)
-1