The choice of floor vs ceiling is essentially arbitrary, as long as
its consistent; using truncation or rounding is mathematically
unsound.  Most implementations use floor, though.

So in general I would expect these to hold:

x div y = floor(x/y)
x mod y = x - y * floor(x/y)

Most importantly, this means that -3 mod 5 should yield 2, not -3.
Perl5, Python, and Ruby get this right; Java, JavaScript, and PHP do
not.  (C's behavior in this regard is theoretically
implementation-dependent but most implementations return the negative

I would also expect the modulus operator to be generalized to
non-integers, since there is nothing in the above formulae that
requires integral inputs.  f mod 1 would then return the fractional
part of a number, for instance.


Reply via email to