Arithmetic overflow in rem and mod

David Feuer david.feuer at gmail.com
Mon Jun 1 17:23:55 UTC 2015


I think this is a mistake, yes. They should not raise such exceptions, but
rather just wrap around—minBound `quot` (-1) should be -minBound=minBound.
That would justify the behavior of rem and mod, and makes much more sense
than the current behavior for Int as a ring.
On Jun 1, 2015 12:41 PM, "Nikita Karetnikov" <nikita at karetnikov.org> wrote:

> 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
> _______________________________________________
> Glasgow-haskell-users mailing list
> Glasgow-haskell-users at haskell.org
> http://mail.haskell.org/cgi-bin/mailman/listinfo/glasgow-haskell-users
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/glasgow-haskell-users/attachments/20150601/157e2b28/attachment.html>


More information about the Glasgow-haskell-users mailing list