Hello Esteban and welcome! On 08/10/12 09:49, Esteban Izaguirre wrote:
So, i undertand how modulo works when only positive numbers are used, but how does modulo determine, that, say -15 % 14 is equal to 13?
Think of modulo as almost exactly the same as "remainder after division". When you say "17 % 5", 5 goes into 17 three times with remainder two, so 17 % 5 returns 2. With negative numbers it works the same way, but with a twist: we could say EITHER: a) 5 goes into -17 negative-three times with remainder negative-two; b) 5 goes into -17 negative-four times with remainder three. Why? Because we can write either of these: 5*-3 + -2 = -17 5*-4 + 3 = -17 So we have a choice. In Python's case, the people who invented the language chose the second one, b). That way the modulo is always positive or zero and never negative, which is a useful thing to have. So, back to your two examples: -2*14 + 13 = -15 so fourteen goes into negative-fifteen -2 times, with 13 remaining. = -20 -20 % 100 is 80, not 20 like you said: -1*100 + 80 = -20 so 100 goes into negative-twenty -1 times, with 80 remaining. -- Steven Or -20 % 100 is 20? I just
don't get how modulo works, all explanations I've found online only seem to be in relation of how this applies to perl or something, can someone explain it to me? _______________________________________________ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
_______________________________________________ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor