Safe Haskell | Safe-Inferred |
---|---|
Language | Haskell2010 |
Synopsis
- data Rational
- unsafeRatio :: Integer -> Integer -> Rational
- fromInteger :: Integer -> Rational
- ratio :: Integer -> Integer -> Maybe Rational
- numerator :: Rational -> Integer
- denominator :: Rational -> Integer
- round :: Rational -> Integer
- truncate :: Rational -> Integer
- properFraction :: Rational -> (Integer, Rational)
- recip :: Rational -> Rational
- abs :: Rational -> Rational
- negate :: Rational -> Rational
- half :: Rational
- fromGHC :: Rational -> Rational
- toGHC :: Rational -> Rational
- gcd :: Integer -> Integer -> Integer
Type
Represents an arbitrary-precision ratio.
The following two invariants are maintained:
- The denominator is greater than zero.
- The numerator and denominator are coprime.
Instances
Construction
ratio :: Integer -> Integer -> Maybe Rational Source #
Safely constructs a Rational
from a numerator and a denominator. Returns
Nothing
if given a zero denominator.
Other functionality
denominator :: Rational -> Integer Source #
Returns the denominator of its argument. This will always be greater than, or equal to, 1, although the type does not describe this.
Note
It is not true in general that
; this
will only hold if denominator
<$>
ratio
x y = yx
and y
are coprime. This is due to Rational
normalizing the numerator and denominator.
truncate :: Rational -> Integer Source #
Returns the whole-number part of its argument, dropping any leftover
fractional part. More precisely,
where truncate
r = n(n, _) =
, but is much more efficient.properFraction
r
properFraction :: Rational -> (Integer, Rational) Source #
returns the pair properFraction
r(n, f)
, such that all of the
following hold:
;fromInteger
n+
f = rn
andf
both have the same sign asr
; and
.abs
f<
one
abs :: Rational -> Rational Source #
Returns the absolute value of its argument.
Note
This is specialized for Rational
; use this instead of the generic version
in PlutusTx.Numeric
, as said generic version produces much larger on-chain
code than the specialized version here.
negate :: Rational -> Rational Source #
Produces the additive inverse of its argument.
Note
This is specialized for Rational
; use this instead of the generic version
of this function, as it is significantly smaller on-chain.
fromGHC :: Rational -> Rational Source #
Converts a GHC Rational
, preserving value. Does not work on-chain.