plutus-tx-1.0.0.0: Libraries for Plutus Tx and its prelude
Safe Haskell None
Language Haskell2010

PlutusTx.Ratio

Synopsis

Type

data Rational Source #

Represents an arbitrary-precision ratio.

Instances

Instances details
Eq Rational Source #
Instance details

Defined in PlutusTx.Ratio

Ord Rational Source #
Instance details

Defined in PlutusTx.Ratio

Show Rational Source #
Instance details

Defined in PlutusTx.Ratio

ToJSON Rational Source #

This mimics the behaviour of Aeson's instance for Rational .

Instance details

Defined in PlutusTx.Ratio

FromJSON Rational Source #

This mimics the behaviour of Aeson's instance for Rational .

Instance details

Defined in PlutusTx.Ratio

Eq Rational Source #
Instance details

Defined in PlutusTx.Ratio

Ord Rational Source #
Instance details

Defined in PlutusTx.Ratio

MultiplicativeMonoid Rational Source #
Instance details

Defined in PlutusTx.Ratio

MultiplicativeSemigroup Rational Source #
Instance details

Defined in PlutusTx.Ratio

AdditiveGroup Rational Source #
Instance details

Defined in PlutusTx.Ratio

AdditiveMonoid Rational Source #
Instance details

Defined in PlutusTx.Ratio

AdditiveSemigroup Rational Source #
Instance details

Defined in PlutusTx.Ratio

UnsafeFromData Rational Source #
Instance details

Defined in PlutusTx.Ratio

FromData Rational Source #
Instance details

Defined in PlutusTx.Ratio

ToData Rational Source #
Instance details

Defined in PlutusTx.Ratio

Lift DefaultUni Rational Source #
Instance details

Defined in PlutusTx.Ratio

Module Integer Rational Source #
Instance details

Defined in PlutusTx.Ratio

Typeable DefaultUni Rational Source #
Instance details

Defined in PlutusTx.Ratio

Construction

unsafeRatio :: Integer -> Integer -> Rational Source #

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.

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

numerator :: Rational -> Integer Source #

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 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 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 Source #

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 Source #

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 ) Source #

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

recip :: Rational -> Rational Source #

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 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.

toGHC :: Rational -> Rational Source #

Converts a Rational to a GHC Rational , preserving value. Does not work on-chain.

reduce :: Integer -> Integer -> Rational Source #

Given a numerator and denominator, produces a Rational by dividing both numerator and denominator by their greatest common divisor.

gcd :: Integer -> Integer -> Integer Source #

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 .