On Saturday, 11 August 2012 at 13:48:16 UTC, David wrote:
-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.
From the language spec:
"For integral operands of the / and % operators, the quotient
rounds towards zero and the remainder has the same sign as the
dividend."
http://dlang.org/expression.html
In your case, the dividend is -1, so the remainder has the same
sign (-ve). The quotient rounds towards zero, so in this case the
quotient is zero, so the remainder must be -1.
Different programming languages handle it differently. In C and
C++ it is implementation defined! (C++11 makes it the same as in
D)
See: http://en.wikipedia.org/wiki/Modulo_operation