Can anyone explain this output? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 3

Can anyone explain this output?

print(-9%7)=5 print(-5%2)=1 But how can you explain this.

8th Nov 2018, 2:58 PM
Maninder $ingh
Maninder $ingh - avatar
1 Answer
+ 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
8th Nov 2018, 3:28 PM
Leif
Leif - avatar