negative integer division

Mark Jackson mjackson at alumni.caltech.edu
Mon Feb 7 19:32:47 EST 2005


Imbaud Pierre <pierre.imbaud at wanadoo.fr> writes:
> integer division and modulo gives different results in c and python, 
> when negative numbers
> are involved. take gdb as a widely available c interpreter
>   print -2 /3
> 0 for c, -1 for python.
> more amazing, modulos of negative number give negative values! (in c).
> from an algebraic point of view, python seems right, but I thought 
> python conformity to the underlying c compiler was a strong commitment, 

AIUI the C standard is silent on the issue, and hence the C behavior is
implementation-dependent.  Anyway back in 2000 I found and fixed a
Y2K-related problem in an open-source C program (xvtdl) which was down
to precisely this misbehavior.  While diagnosing the problem I
implemented the algorithm in Python for test purposes, and was led
astray for a while by the fact that it *didn't* fail!

A:  42

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

-- 
Mark Jackson - http://www.alumni.caltech.edu/~mjackson
	People who write obscurely are either unskilled in writing
	or up to mischief.		- Sir Peter Medawar





More information about the Python-list mailing list