I know this has been addressed here, but I confess to not fully understanding that, so I'm hoping someone can chime here.
First, is there a canonical formula for this? In programming language languages, different ones lead to varying results, which would seem to indicate not?
Anyway, in layman's terms, I've always understood modulo to mean something like:
- Fractional remainder * quotient = x
- ceiling(x) = result
So an example with 5 mod 3:
- 5/3 = 1.66666666...
- fractional = 0.666666 * 3 = 1.998
- result = ceiling(1.998) = 2
applying that to a negative number though, like -1 mod 18:
- -1/18 = -0.0555555
- fractional = -0.055555 * 18 = -0.99999
- result = ceiling(-0.99999) = 0
- This seems wrong, so maybe floor() makes sense for negatives, which would yield -1 as the result
(#)4 seems intuitively correct as positive 1 mod 18 = 1. But that's wrong, as it seems -1 mod 18 = 17, and I just don't see how to get to that.
Thanks!