Modulo operator : differences between C and Python

jcm grumble at lycos.com
Mon Mar 11 08:04:51 EST 2002


A better name might be rem, as this produces the remainder of x/y.
Python returns what most would call the modulus.

Cimarron Taylor <cimarron+google at taylors.org> wrote:
>> Anybody have any idea why this is?
>> Erik

> I encountered this as well in an application which needed to
> verify the results of a C program.  Python seems to be counting
> "down" from y if x<0.  I simply compensated for this with
> the following function:  

>         def mod(x,y):
>             m = x%y
>             if m==0 or x>0: return m
>             return m-y

> Another thing to watch out for when comparing results from Python and
> C include is overflow/underflow behavior of the math operations.

> Cim



More information about the Python-list mailing list