Integer math question

Sean Ross sross at connectmail.carleton.ca
Mon Jan 5 20:04:25 EST 2004


"Samuel Walters" <swalters_usenet at yahoo.com> wrote in message
news:pan.2004.01.05.17.54.22.894833 at yahoo.com...
> "If a and b are integers such that b != 0, then there exist unique
> integers r and q such that a = q*b + r and 0 <= r < abs(b)"
>
> For non-mathematical spectators, the divmod() function is defined so that
> q, r = divmod(a, b) by the definition above.

Right. The only thing that was still mildly interesting to me was why does
divmod() return a negative remainder?

>>> a = 5
>>> b = -10
>>> q,r = divmod(a,b)
>>> q
-1
>>> r
-5

If divmod() where defined based on the definition above, then divmod(5, -10)
should return (0, 5).
Well ... that's too strong. The above is a theorem - it doesn't say
remainders have to be nonnegative,
it only says that there exist yada yada yada ... whatever,  I'm not that
interested.






More information about the Python-list mailing list