-10 // 3 = -4
10 // -3 = -4
-10 % 3 = 2
10 % -3 = -2
I don't get the logic of these operations.
10 // 3 = 3, but -10 // 3 = -4
which I cant make sense of.
Please, explain.
-10 // 3 = -4
10 // -3 = -4
-10 % 3 = 2
10 % -3 = -2
I don't get the logic of these operations.
10 // 3 = 3, but -10 // 3 = -4
which I cant make sense of.
Please, explain.
This is Because python rounds down. 3.33333 rounded down is 3, wheras -3.33333 rounded down is -4. If you want it to round up, do floating point division, then convert the float to an int.
The operator //
is the floor division operator.
The result of -10/3
is -3.3333
. Then, the result of -10//3
will be rounded to the next integer (lower than the result), so the result will be -4
.