Ratio package:numhask

A rational number, represented as the ratio of two Integral numbers.
Ratio of two integers
Rational classes
Fractional in base splits into fromRatio and Field
>>> fromRatio (5 :% 2 :: Ratio Integer) :: Double
2.5
fromRational is special in two ways:
  • numeric decimal literals (like "53.66") are interpreted as exactly "fromRational (53.66 :: GHC.Real.Ratio Integer)". The prelude version, GHC.Real.fromRational is used as default (or whatever is in scope if RebindableSyntax is set).
  • The default rules in haskell2010 specify that contraints on fromRational need to be in a form C v, where v is a Num or a subclass of Num.
So a type synonym of `type FromRational a = FromRatio a Integer` doesn't work well with type defaulting; hence the need for a separate class.
toRatio is equivalent to Real in base, but is polymorphic in the Integral type.
>>> toRatio (3.1415927 :: Float) :: Ratio Integer
13176795 :% 4194304