plutus-tx-1.60.0.0: Libraries for Plutus Tx and its prelude
Safe HaskellSafe-Inferred
LanguageHaskell2010

PlutusTx.Ratio

Synopsis
type Rep Rational # 
Instance details

Defined in PlutusTx.Ratio

type Unroll Rational # 
Instance details

Defined in PlutusTx.Ratio

Construction

unsafeRatio :: Integer -> Integer -> Rational #

Makes a Rational from a numerator and a denominator.

Important note

If given a zero denominator, this function will error. If you don't mind a size increase, and care about safety, use ratio instead.

fromInteger :: Integer -> Rational #

Converts an Integer into the equivalent Rational.

ratio :: Integer -> Integer -> Maybe Rational #

Safely constructs a Rational from a numerator and a denominator. Returns Nothing if given a zero denominator.

Other functionality

numerator :: Rational -> Integer #

Returns the numerator of its argument.

Note

It is not true in general that numerator <$> ratio x y = x; this will only hold if x and y are coprime. This is due to Rational normalizing the numerator and denominator.

denominator :: Rational -> Integer #

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 denominator <$> ratio x y = y; this will only hold if x and y are coprime. This is due to Rational normalizing the numerator and denominator.

round :: Rational -> Integer #

round r returns the nearest Integer value to r. If r is equidistant between two values, the even value will be given.

truncate :: Rational -> Integer #

Returns the whole-number part of its argument, dropping any leftover fractional part. More precisely, truncate r = n where (n, _) = properFraction r, but is much more efficient.

properFraction :: Rational -> (Integer, Rational) #

properFraction r returns the pair (n, f), such that all of the following hold:

recip :: Rational -> Rational #

Gives the reciprocal of the argument; specifically, for r /= zero, r * recip r = one.

Important note

The reciprocal of zero is mathematically undefined; thus, recip zero will error. Use with care.

abs :: Rational -> Rational #

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 #

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.

gcd :: Integer -> Integer -> Integer #

gcd x y is the non-negative factor of both x 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 4 = 4. gcd 0 0 = 0.

Conversion from/to Haskell

fromHaskellRatio :: Rational -> Rational #

Converts a GHC Rational, preserving value.

Note: Does not work on-chain.

toHaskellRatio :: Rational -> Rational #

Converts a Rational to a GHC Rational, preserving value.

Note: Does not work on-chain.

fromGHC :: Rational -> Rational #

Deprecated: Use fromHaskellRatio instead

toGHC :: Rational -> Rational #

Deprecated: Use toHaskellRatio instead