# Modul (%) in python not like in C?

mensanator at aol.com mensanator at aol.com
Sun Sep 9 22:12:09 CEST 2007

```On Sep 9, 2:15ï¿½pm, "J. Cliff Dyer" <j... at sdf.lonestar.org> wrote:
> Dotan Cohen wrote:
> > 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.
>
> > Dotan Cohen
>
> The % operator is called "modulo" in English.  I don't think the
> difference in implementation is an error.  It's just a difference of
> calculation method.
>
> Python will always yield a number x = m%n such that 0 <= x < n, but
> Turbo C will always yield a number such that if x = m%n -x = -m%n.  That
> is, since 111 % 10 = 1, -111 % 10 = -1.  The two values will always
> differ by n (as used above).
>
> I'm sure there are mathematicians on the list who can give you a more
> technical, precise explanation of the reasons for the different results.
>
> Cheers,
> Cliff

Think of clock arithmetic:

/   0
-1 /
/ 9       1

8           2

7           3

6       4

5

1 step counter-clockwise is the same as 9 steps
clockwise, so -1 is congruent to 9 (mod 10), so both
are correct. A common mistake is to assume -1 and +1
to be the same. Remember, in Python, sequential
negative numbers % n is equivalent to going
counter-clockwise:

>>> for i in xrange(-1,-12,-1):
print i % 12,

11 10 9 8 7 6 5 4 3 2 1

```