Arithmetic overflow in rem and mod

Nikita Karetnikov nikita at karetnikov.org
Mon Jun 1 16:40:55 UTC 2015


According to the documentation, rem and mod must satisfy the following
laws:

    -- > (x `quot` y)*y + (x `rem` y) == x
    rem

    -- > (x `div` y)*y + (x `mod` y) == x
    mod

https://hackage.haskell.org/package/base-4.8.0.0/docs/src/GHC-Real.html

Note, however, that there is a case when quot and div result in an
arithmetic overflow:

Prelude> (minBound :: Int) `quot` (-1)
*** Exception: arithmetic overflow
Prelude> (minBound :: Int) `div` (-1)
*** Exception: arithmetic overflow

while rem and mod don't:

Prelude> (minBound :: Int) `rem` (-1)
0
Prelude> (minBound :: Int) `mod` (-1)
0

Is this a mistake?

For the record, I'm aware of the safeint package, which raises the error
for rem and mod, and this ticket:

https://ghc.haskell.org/trac/ghc/ticket/8695


More information about the Glasgow-haskell-users mailing list