Modul (%) in python not like in C?

Arnaud Delobelle arnodel at googlemail.com
Mon Sep 10 08:13:55 CEST 2007

```On Sep 9, 10:06 pm, stef mientki <stef.mien... at gmail.com> wrote:
> J. Cliff Dyer 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.
>
> It's a long time ago, but if I remember well,
> "modulo" was introduced as an operator in Galois fields theory,
> and Galois fields only exists of the whole numbers 0 .. N-1.

You're probably thinking of Galois *groups*.  Some finite Galois
groups are cyclic, so they are of the form Z/nZ that can be described
as {0, .. , n-1}.  But Galois groups can have many other forms.

--
Arnaud

```