[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
div and mod.
For what it's worth, here is the behavior that I as a programmer
expect from Div and Mod.
I expect mod always to return a number between zero inclusive and the
modulus exclusive. IOW, for all real X:
(mod X 3) -> always a number between 0 and 3
(mod X -3) -> always a number between 0 and -3.
I expect div to return whatever has to be returned to assure that
for all real x and real N,
(+ (mod x N) (* (div x N) N)) -> x
It happens that in all cases where the signs match, the
expected sign of the result of div is positive or zero. In
all cases where the signs don't match, the expected sign of
the result of div is negative or zero.
In many systems they behave otherwise. When that happens, it
frequently causes bugs which I eventually trace down to the
application of div or modulus returning a negatve (or positive!)
number unexpectedly, and after some experimentation kluge
together a workaround or correction for.