On Saturday, 11 August 2012 at 17:15:21 UTC, Norbert Nemec wrote:
On 11.08.2012 18:13, bearophile wrote:
David:
Thanks! I thought modulo should alawys yield the same ...
seems like I
was wrong ;)
It's C design that's wrong.
And it's the processor design that makes it inefficient to
correct it nowadays.
Python's definition of modulo is far more useful than C's.
Implemented in machine code, however, it takes several
additional commands because the integer division is hardwired
to the C definition. I guess that hardware integer division in
processors became popular only when C was already widely in use.
A few extra instructions (a CMOV followed by ADD should suffice,
yes?) seems like a small price to pay if it can prevent bugs. Why
hasn't the Python-modulo been made the default back when D was
designed? The ever-so-slightly more efficient C-modulo could be
provided in a library. Of course it's way too late to change it
now...