Difference between `mod` and `rem` in Haskell
They're not the same when the second argument is negative:
2 `mod` (-3) == -1
2 `rem` (-3) == 2
Yes, those functions act differently. As defined in the official documentation:
quot
is integer division truncated toward zero
rem
is integer remainder, satisfying:
(x `quot` y)*y + (x `rem` y) == x
div
is integer division truncated toward negative infinity
mod
is integer modulus, satisfying:
(x `div` y)*y + (x `mod` y) == x
You can really notice the difference when you use a negative number as second parameter and the result is not zero:
5 `mod` 3 == 2
5 `rem` 3 == 2
5 `mod` (-3) == -1
5 `rem` (-3) == 2
(-5) `mod` 3 == 1
(-5) `rem` 3 == -2
(-5) `mod` (-3) == -2
(-5) `rem` (-3) == -2