Modulo operator : differences between C and Python

Geoff Gerrietts geoff at gerrietts.net
Sun Mar 10 18:07:01 EST 2002


Quoting Erik de Castro Lopo (nospam at mega-nerd.com):
> 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.

I ran into this very same thing the other day. I was setting my
expectations of how a C expression would evaluate by testing it in the
python shell, only to discover that it evaluated quite differently.

I don't think it bothered me. Just surprised me. Turns out what I
really wanted was the Python answer anyway. I'm not sure the answer I
got in C is actually the modulo, if I understand the mathematical
concept "modulo" properly.

-- 
Geoff Gerrietts             "I am always doing that which I can not do, 
<geoff at gerrietts net>     in order that I may learn how to do it." 
http://www.gerrietts.net                    --Pablo Picasso




More information about the Python-list mailing list