Safe Haskell | Safe-Inferred |
---|---|
Language | Haskell2010 |
Rational classes
Documentation
A rational number
!a :% !a |
Instances
class ToRatio a b where Source #
toRatio is equivalent to Real
in base, but is polymorphic in the Integral type.
>>>
toRatio (3.1415927 :: Float) :: Ratio Integer
13176795 :% 4194304
Instances
class FromRatio a b where Source #
Fractional
in base splits into fromRatio and Field
>>>
fromRatio (5 :% 2 :: Ratio Integer) :: Double
2.5
class FromRational a where Source #
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 formC 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.
fromRational :: Rational -> a Source #
Instances
FromRational Double Source # | |
Defined in NumHask.Data.Rational fromRational :: Rational -> Double Source # | |
FromRational Float Source # | |
Defined in NumHask.Data.Rational fromRational :: Rational -> Float Source # | |
FromRational (Ratio Integer) Source # | |
Defined in NumHask.Data.Rational |
reduce :: (Eq a, Subtractive a, Signed a, Integral a) => a -> a -> Ratio a Source #
reduce
normalises a ratio by dividing both numerator and denominator by
their greatest common divisor.
>>>
reduce 72 60
6 :% 5
\a b -> reduce a b == a :% b || b == zero
gcd :: (Eq a, Signed a, Integral a) => a -> a -> a Source #
is the non-negative factor of both gcd
x yx
and y
of which
every common factor of x
and y
is also a factor; for example
, gcd
4 2 = 2
, gcd
(-4) 6 = 2
= gcd
0 44
.
= gcd
0 00
.
(That is, the common divisor that is "greatest" in the divisibility
preordering.)
Note: Since for signed fixed-width integer types,
,
the result may be negative if one of the arguments is abs
minBound
< 0
(and
necessarily is if the other is minBound
0
or
) for such types.minBound
>>>
gcd 72 60
12