News for hackers. Stuff that matters.
Re:Mod and remainder are not the same by jacobolus on Thursday August 23, 2018 @11:05AM (#17827832)
No, 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)
Re:Mod and remainder are not the same by Skeime on Friday August 24, 2018 @09:55AM (#17834934)
Grandparent 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.