Integer division and remainder

Paul Rubin phr-n2001 at nightsong.com
Wed Aug 15 05:48:59 EDT 2001


Marcin 'Qrczak' Kowalczyk <qrczak at knm.org.pl> writes:
> > 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).

That means you want % to mean "modulo" rather than "remainder".  

Anyway C apparently doesn't specify a behavior.  It leaves it up to
the machine arithmetic.  Practically all machine arithmetic rounds towards 0.
Also, the built-in Python int() function rounds towards 0.  So you have
int(-7.0/3.0) giving a different result than int(-7.0)/int(3.0), which
is kind of strange.

I haven't figured out any actual practical bad consequences of Python's
implementation but I keep thinking there might be some.



More information about the Python-list mailing list