163

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
Oscar Mederos
  • 29,016
  • 22
  • 84
  • 124

7 Answers7

212

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

2 `mod` (-3)  ==  -1
2 `rem` (-3)  ==  2
Fred Foo
  • 355,277
  • 75
  • 744
  • 836
  • 23
    I had the same question about `rem` and `mod` in Clojure, and this was the answer. – noahlz Jul 11 '12 at 15:29
  • 12
    Nor are they the same when the first argument is negative. See http://stackoverflow.com/a/8111203/1535283 and http://stackoverflow.com/a/339823/1535283 for some more info about these tricky operations. – Scott Olson Apr 10 '13 at 09:22
  • 4
    Also from http://stackoverflow.com/a/6964760/205521 it seems like `rem` is fastest. – Thomas Ahle Sep 28 '14 at 10:53
  • 23
    Though this answer is correct, an answer claiming no more than "not the same" to a question "what is the difference" is a very bad one. I would welcome it if you could expand on "how" they are different and some usecases probably. – poitroae Jan 13 '15 at 10:35
79

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

 

Erik Hesselink
  • 2,420
  • 21
  • 25
Giuseppe Bertone
  • 2,062
  • 15
  • 15
23

Practically speaking:

If you know both operands are positive, you should usually use quot, rem, or quotRem for efficiency.

If you don't know both operands are positive, you have to think about what you want the results to look like. You probably don't want quotRem, but you might not want divMod either. The (x `div` y)*y + (x `mod` y) == x law is a very good one, but rounding division toward negative infinity (Knuth style division) is often less useful and less efficient than ensuring that 0 <= x `mod` y < y (Euclidean division).

dfeuer
  • 48,079
  • 5
  • 63
  • 167
9

In case you only want to test for divisibility, you should always use rem.

Essentially x `mod` y == 0 is equivalent to x `rem` y == 0, but rem is faster than mod.

sjakobi
  • 3,546
  • 1
  • 25
  • 43
  • Why is `rem` fastern them `mod`? – hr0m May 16 '21 at 18:02
  • 2
    At least in the case of `Int` and `Integer`, `mod` is implemented in terms of `rem`, but requires some extra checks: http://hackage.haskell.org/package/ghc-prim-0.7.0/docs/src/GHC-Classes.html#modInt%23, http://hackage.haskell.org/package/integer-gmp-1.0.3.0/docs/src/GHC.Integer.Type.html#modInteger – sjakobi May 18 '21 at 12:15
3
quotRem' a b = (q, r) where
    q = truncate $ (fromIntegral a / fromIntegral b :: Rational)
    r = a - b * q
divMod'  a b = (q, r) where
    q = floor    $ (fromIntegral a / fromIntegral b :: Rational)
    r = a - b * q

ex:

(-3) / 2 = -1.5
(-3) `quot` 2 = truncate (-1.5) = -1
(-3) `div`  2 = floor    (-1.5) = -2
(-3) `rem` 2 = -3 - 2 * (-1) = -1
(-3) `mod` 2 = -3 - 2 * (-2) = 1

3 / (-2) = -1.5
3 `quot` (-2) = truncate (-1.5) = -1
3 `div`  (-2) = floor    (-1.5) = -2
3 `rem` (-2) = 3 - (-2) * (-1) = 1
3 `mod` (-2) = 3 - (-2) * (-2) = -1
sp55aa
  • 51
  • 3
  • While this code may solve the question, [including an explanation](//meta.stackexchange.com/q/114762) of how and why this solves the problem would really help to improve the quality of your post, and probably result in more up-votes. Remember that you are answering the question for readers in the future, not just the person asking now. Please [edit] your answer to add explanations and give an indication of what limitations and assumptions apply. – Adrian Mole Sep 15 '21 at 12:08
1

This is a graph of haskell's mod and rem over [-20,20] integer range:

enter image description here

  • That's useful, but the captions are rather confusing and the colours hard to keep apart. Three individual plots and a line of actual Haskell code for each would be much better to understand. – leftaroundabout Jan 16 '23 at 14:12
0

I cannot upload an image to explain it. But you can draw it yourself.

suppose :

X = mod(a,b)  ; Y = rem(a,b)

---(-(n+1)b)---a---(-nb)---.......--(-2b)-----(-b)-----0-----b--->

X = a - [ -(n+1)b ]  

so that X is always positive

Y = a - [ -nb ]  

in standard documentation:

mod  -->  a - b.*floor(a./b).......floor is closer to negative infinity

rem  -->  a - b.*fix(a./b).........fix is closer to 0
Suraj Rao
  • 29,388
  • 11
  • 94
  • 103
Harvey
  • 1