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

PlutusTx.Ratio

Synopsis

Type

data Rational Source #

Represents an arbitrary-precision ratio.

The following two invariants are maintained:

  1. The denominator is greater than zero.
  2. The numerator and denominator are coprime.

Instances

Instances details
FromJSON Rational Source #

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

Instance details

Defined in PlutusTx.Ratio

Methods

parseJSON :: Value -> Parser Rational

parseJSONList :: Value -> Parser [Rational]

omittedField :: Maybe Rational

ToJSON Rational Source #

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

Instance details

Defined in PlutusTx.Ratio

Methods

toJSON :: Rational -> Value

toEncoding :: Rational -> Encoding

toJSONList :: [Rational] -> Value

toEncodingList :: [Rational] -> Encoding

omitField :: Rational -> Bool

Generic Rational Source # 
Instance details

Defined in PlutusTx.Ratio

Associated Types

type Rep Rational :: Type -> Type Source #

Show Rational Source # 
Instance details

Defined in PlutusTx.Ratio

Eq Rational Source # 
Instance details

Defined in PlutusTx.Ratio

Ord Rational Source # 
Instance details

Defined in PlutusTx.Ratio

HasBlueprintDefinition Rational Source # 
Instance details

Defined in PlutusTx.Ratio

Associated Types

type Unroll Rational :: [Type] Source #

Eq Rational Source # 
Instance details

Defined in PlutusTx.Ratio

Methods

(==) :: Rational -> Rational -> Bool Source #

FromData Rational Source # 
Instance details

Defined in PlutusTx.Ratio

ToData Rational Source # 
Instance details

Defined in PlutusTx.Ratio

UnsafeFromData 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

Methods

zero :: Rational Source #

AdditiveSemigroup Rational Source # 
Instance details

Defined in PlutusTx.Ratio

MultiplicativeMonoid Rational Source # 
Instance details

Defined in PlutusTx.Ratio

Methods

one :: Rational Source #

MultiplicativeSemigroup Rational Source # 
Instance details

Defined in PlutusTx.Ratio

Ord Rational Source # 
Instance details

Defined in PlutusTx.Ratio

Pretty Rational Source # 
Instance details

Defined in PlutusTx.Ratio

Methods

pretty :: Rational -> Doc ann

prettyList :: [Rational] -> Doc ann

HasSchemaDefinition Integer referencedTypes => HasBlueprintSchema Rational referencedTypes Source # 
Instance details

Defined in PlutusTx.Ratio

Methods

schema :: Schema referencedTypes Source #

Lift DefaultUni Rational Source # 
Instance details

Defined in PlutusTx.Ratio

Methods

lift :: Rational -> RTCompile DefaultUni fun (Term TyName Name DefaultUni fun ()) Source #

Module Integer Rational Source # 
Instance details

Defined in PlutusTx.Ratio

Typeable DefaultUni Rational Source # 
Instance details

Defined in PlutusTx.Ratio

Methods

typeRep :: Proxy Rational -> RTCompile DefaultUni fun (Type TyName DefaultUni ()) Source #

type Rep Rational Source # 
Instance details

Defined in PlutusTx.Ratio

type Rep Rational = D1 ('MetaData "Rational" "PlutusTx.Ratio" "plutus-tx-1.36.0.0-8y1X6QaF1WM5mIhDTsyzym" 'False) (C1 ('MetaCons "Rational" 'PrefixI 'False) (S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedStrict) (Rec0 Integer) :*: S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedStrict) (Rec0 Integer)))
type Unroll 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.

fromInteger :: Integer -> Rational Source #

Converts an Integer into the equivalent Rational.

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.

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.