cometa-0.1.0.0: Idiomatic Haskell bindings to cardano-c
Safe HaskellSafe-Inferred
LanguageHaskell2010

Cometa.Protocol.Rational

Description

Exact nonnegative protocol fractions, with positive denominators. Numerators and denominators are unsigned 64-bit values. Input pairs remain unreduced for serialization; equality and ordering compare numeric values.

mkNonNegativeRational 577 10000 constructs an exact execution price.

Synopsis

Documentation

data NonNegativeRational Source #

Exact nonnegative fraction with an unsigned 64-bit numerator and positive unsigned 64-bit denominator. The original pair is retained for encoding; equality and ordering compare numeric values. Show is diagnostic.

mkNonNegativeRational :: Integer -> Integer -> Either Error NonNegativeRational Source #

Validate an unsigned 64-bit numerator and positive unsigned 64-bit denominator. Retain the exact pair. Invalid values return InvalidProtocolRational.

fromDouble :: Double -> Either Error NonNegativeRational Source #

Convert a finite nonnegative Double using the native decimal-floor rule on its fractional part, then add the whole part. Reduce that fractional pair before adding the whole part, retaining native numerator/denominator choices. Nonfinite, negative or out-of-uint64 whole parts return InvalidProtocolDouble. If the resulting exact numerator exceeds uint64, return InvalidProtocolRational instead of reproducing native unsigned overflow.

toDouble :: NonNegativeRational -> Double Source #

Convert numerator and denominator separately to Double and divide, matching the native approximation without changing the stored exact pair.

numerator :: NonNegativeRational -> Word64 Source #

Original numerator, without fraction reduction.

denominator :: NonNegativeRational -> Word64 Source #

Original positive denominator, without fraction reduction.

toRational :: NonNegativeRational -> Rational Source #

Exact numeric value, reduced by the standard Rational representation.

encodeCbor :: NonNegativeRational -> ByteString Source #

Encode a definite-length ledger CBOR value with shortest integer encodings.

decodeCbor :: ByteString -> Either Error NonNegativeRational Source #

Decode exactly one ledger CBOR value. Invalid fields or trailing data return InvalidProtocolCbor. Nonminimal integers and indefinite arrays are accepted; output encoding uses definite arrays and shortest integers.