var x: i32 = -17 % 12; //compile error: remainder division with '(integer literal)' and '(integer literal)': signed integers and floats must use @rem or @mod
var y: i32 = @mod(-17, 12); //7
var z: i32 = @rem(-17, 12); //-5Correct me if I'm wrong, but this is also attractive because it can be applied even to an "actual modulo" operator. That is, because "(x mod n + n) mod n" has the same behavior as the above, one need not even know which way a particular language's "%" operator behaves if one just always uses this filter.
The case of a negative denominator doesn't seem to make sense when talking about modulo in an algebraic sense. I don't think you can have a Ring with a negative dimension. So I've never even thought about what % would do with a negative denominator (RHS). If I was using % with an unknown denominator as input, I could just as easily end up with it being 0 which would be a bad thing, so I think it's fair to say I always require it to be positive.
To the grandparent:
> sends each number to a special representative of its equivalence class
The problem is that many programming languages have a “remainder” type % operator which does not do this in the case where the dividend is negative. For instance,
-1 % 3 == -1
2 % 3 == 2
Instead, folks who want mathematically reasonable behavior need to implement their own. For instance in Javascript we can define mod = (a, b) => a - b*Math.floor(a/b)I tend to define
mod = (a, b) => (a % b + b) % b
because it works without going through floats – in languages that have integer types. I don’t know if there are actual advantages though.[0]: https://en.wikipedia.org/wiki/Modulo_operation#Remainder_cal...
However, the real distinction lies in how the two operations handle negative dividends: If n is positive, the modulo operator
always returns a number between 0 and n – 1. The remainder operator returns a number between –n + 1 and 0 if x is negative instead. For example, and I have yet to see a case where the remainder operator is the one that you want. If a language only supports rem I find myself constantly writing (which is just