Difference between `mod` and `rem` in Haskell

Oscar Mederos picture Oscar Mederos · May 5, 2011 · Viewed 83.5k times · Source

What exactly is the difference between mod and rem in Haskell?

Both seems to give the same results

*Main> mod 2 3
2
*Main> rem 2 3
2
*Main> mod 10 5
0
*Main> rem 10 5
0
*Main> mod 1 0
*** Exception: divide by zero
*Main> rem 1 0
*** Exception: divide by zero
*Main> mod 1 (-1)
0
*Main> rem 1 (-1)
0

Answer

Fred Foo picture Fred Foo · May 5, 2011

They're not the same when the second argument is negative:

2 `mod` (-3)  ==  -1
2 `rem` (-3)  ==  2