Modulo Bug?

Alex Rønne Petersen alex at lycus.org
Sun Aug 12 01:43:53 PDT 2012


On 11-08-2012 20:54, Thiez wrote:
> 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...

Keep in mind that not all CPUs have cmov, so you end up having to branch 
based on whether the CPU has it or not.

-- 
Alex Rønne Petersen
alex at lycus.org
http://lycus.org


More information about the Digitalmars-d mailing list