Modul (%) in python not like in C?
tjreedy at udel.edu
Sun Sep 9 23:07:37 CEST 2007
"Dotan Cohen" <dotancohen at gmail.com> wrote in message
news:880dece00709091201n449d4c2k31998a5d426de17d at mail.gmail.com...
| FIrst of all, how is the % symbol (as in 70%6=4) called in English?
| Second, in Turbo C -111%10=-1 however in python -111%10=9. Is one or
| the other in error? Is this a known gotcha? I tried to google the
| subject however one cannot google the symbol %. Thanks in advance.
Call it a known gotcha inherent to integers. In the realm of counts (aka
non-negative integers), there is one sensible definition of division and
modulus (remainder). It has various nice properties. With mixed positive
and negative integers, there are choices, and none have all the nice
properties. The basic problem is that '-infinity' and 0 are in opposite
directions instead of the same direction ;-). Guido made one consistent
choice (which matches some other languages, I believe). Others have made
another consistent choice or no choice at all (leaving the choice to
implementations). I believe I have seen at least one book, if not
language, that defined // and % so that remainders have the same sign as
the numerator. (Or was it denominator?)
More information about the Python-list