Modulo Bug?

bearophile bearophileHUGS at lycos.com
Sat Aug 11 09:12:07 PDT 2012


David:

> -1 % 16 = -1
>
> Shouldn't that be 15? It seems like the sign is ignored for the 
> modulo.
>
> Is this a bug or intended behaviour? The Python implementation 
> returns here, as expected, 15.

It's a localized but important design bug of languages like C 
that D has carried over for backward compatibility reasons. D % 
has caused bugs in my code. I suggest to not use the built-in % 
on numbers that can be negative, unless you really know what you 
are doing.

Bye,
bearophile


More information about the Digitalmars-d mailing list