Integer division and remainder

Mark Jackson mjackson at wrc.xerox.com
Wed Aug 15 17:18:00 EDT 2001


Robert Amesz <reqhye72zux at mailexpire.com> writes:
> 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.

Yes!  That was the very problem with the old to-do list
manager xvtdl, which I fixed back in early 2000; find the code at
http://www.alumni.caltech.edu/~mjackson/xvtdl.html.  While diagnosing
this behavior I implemented the algorithm in Python for test purposes,
and was led astray for a while by the fact that it *didn't* fail!

Q:  What multiple of 7 did I add to the critical expression in the Zeller
    algorithm so it will remain nonnegative for the next few centuries?

A:  42

-- 
Mark Jackson - http://www.alumni.caltech.edu/~mjackson
	Don't talk to the crazy people on the street, even though
	they may seem fun to be with.
	    - /CityPack, New York/ Chinese language edition (1996)





More information about the Python-list mailing list