[Tutor] modulo
wrw at mac.com
wrw at mac.com
Mon Oct 8 03:43:38 CEST 2012
On Oct 7, 2012, at 6:49 PM, Esteban Izaguirre <estebaniza at gmail.com> wrote:
> Hi, I'm following coursera's learn to program: the fundamentals, which teaches programming basics in python. Our first assignement involves the modulo operator with a negative divident, and while I've managed to get to understand it enough for the purposes of the assignement with help from othe rstudents, I still don't know how the hell it works, I wouldn't know how to use modulo in another situation if it ever arised. 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? 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 at python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
After reading all the other (good) answers, let me try one other way to think about it.
First, for positive integers consider 15/12 = 1 and 15%12 = 3. So 12 * (15/12) + 15%12 = 15 and we are back where we started.
In order to be able to perform the same operations on a negative dividend, it has to work the way you find puzzling.
Consider: -15/12 = -2 and -15%12 = 9, which is the way it has to be in order for 12 * (-15/12) i.e. -24 plus -15%12 i.e. 9 to equal -15.
-Bill
More information about the Tutor
mailing list