Integer division and remainder

Robert Amesz reqhye72zux at mailexpire.com
Wed Aug 15 16:53:34 EDT 2001


Marcin 'Qrczak' Kowalczyk wrote:

> 14 Aug 2001 16:20:57 -0700, Paul Rubin <phr-n2001 at nightsong.com>
> pisze: 
> 
>>    7 / 3 =  2
>>   -7 / 3 = -3
>> 
>> This is kind of strange.  Normally you expect (-a)/b to be the
>> same as -(a/b). That's what happens in most languages, where
>> division rounds towards zero: 
> 
> Donald Knuth says that Python is right and C is wrong, and I agree
> with him here. I expect a%b for positive b to return a value in the
> range [0, b).

You might be interested to know that a well-known and widely used 
formula for calculating the day of the week, Zeller's congruence, 
failed on many systems for dates >= 2000 just because on most systems 
the mod-function gave the wrong results for negative values. On Python 
the problem wouldn't have occurred, its mod-function is mathematically 
correct.


Robert Amesz




More information about the Python-list mailing list