+ 3
Can anyone explain this output?
print(-9%7)=5 print(-5%2)=1 But how can you explain this.
1 Respuesta
+ 10
It depends on the division operator. Quotient q and remainder r relate through the equation : n = q*m + r
Python uses floor division (rounds towards minus infinity). Thus -9 // 7 is -2 and not -1 as is the case for division that round towards zero (for instance C/C++).
So, the remainder is:
r = -9 - 7*(-9//7) = -9 - 7*(-2) = -9 - (-14) = 5
In addition, you may find this article helpful:
https://en.wikipedia.org/wiki/Modulo_operation#Remainder_calculation_for_the_modulo_operation