4 ms·
I recalling seeing these referred to as modp (positive) and mods (symmetric) back in University. This seems to be the clearest way to distinguish them. I didn't
by zeroimpl 8y ago
I recalling seeing these referred to as modp (positive) and mods (symmetric) back in University. This seems to be the clearest way to distinguish them. I didn't realize different popular programming languages used different versions for the % operator...
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.
- Skeime 8y agoWell, algebraically, Z/nZ and Z/(–n)Z are the same. However, you have again some choice of canonical representative (and you might choose differently for n and –n). But I agree that this is a rather rare case.
- jacobolus 8y agoNo, the symmetric version is yet a third variant. What people are calling “rem” is what you get with division that rounds toward zero, whereas what they are calling “mod” is what you get with division that always rounds down. A symmetric version is what you get with division that rounds to the nearest integer, and I have not seen any language with a built-in operator for it. 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)
- Skeime 8y agoGrandparent here: I know, that’s what I tried to explain (poorly) with the next sentences. Programming languages with a remainder operator choose two representatives for every equivalence class except [0] and return the one that has the same sign as the dividend. 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.