Modulo operator : differences between C and Python

David Eppstein eppstein at ics.uci.edu
Sun Mar 10 19:58:49 EST 2002


In article <3C8BE537.74E55963 at mega-nerd.com>,
 Erik de Castro Lopo <nospam at mega-nerd.com> wrote:

> 
> Just recently I prototyped a relatively complex algorithm in Python
> before converting it to C. During this conversion I noticed that
> the modulo operator returns different results in C and Python if
> the numerator is negative. For positive values they produce the same 
> result.

Yes.  Python produces the correct results.  C doesn't.

Almost every time I use modulo in C, I end up having to fix it by adding 
the modulus if the result is negative.

-- 
David Eppstein       UC Irvine Dept. of Information & Computer Science
eppstein at ics.uci.edu http://www.ics.uci.edu/~eppstein/



More information about the Python-list mailing list