Skip to content

Weird behaviour of Ratio Int (Data.Ratio): negative denominators

Summary

For some numbers of type Ratio Int with big denominators >= 3037000500, x-x yields 0 % (-1).

Steps to reproduce

Open up ghci and run:

import Data.Ratio
p = 1 % 3037000500 :: Ratio Int
p - p

gives 0 % (-1),

denominator (p - p)

gives -1, but according to documentation it should give a positive number.

Expected behavior

p - p should produce 0 % 1, just as it does for

q = 1 % 3037000499 :: Ratio Int
q - q

gives 0 % 1.

r = 1 % 3037000500 :: Ratio Integer
r - r

also correctly gives 0 % 1.

Environment

  • GHC version used: GHCi, version 8.10.1

Optional:

  • Operating System: macOS Mojave 10.14.6
  • System Architecture: x86_64

Sample Session

$ ghci
GHCi, version 8.10.1: https://www.haskell.org/ghc/  :? for help
Loaded package environment from /Users/immo/.ghc/x86_64-darwin-8.10.1/environments/default
Prelude> import Data.Ratio
Prelude Data.Ratio> p = 1 % 3037000500 :: Ratio Int
Prelude Data.Ratio> p - p
0 % (-1)
Prelude Data.Ratio> q = 1 % 3037000499 :: Ratio Int
Prelude Data.Ratio> q - q
0 % 1
Prelude Data.Ratio> r = 1 % 3037000500 :: Ratio Integer
Prelude Data.Ratio> r - r
0 % 1
To upload designs, you'll need to enable LFS and have an admin enable hashed storage. More information