| Safe Haskell | Safe-Inferred |
|---|---|
| Language | Haskell2010 |
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
- data NonNegativeRational
- mkNonNegativeRational :: Integer -> Integer -> Either Error NonNegativeRational
- fromDouble :: Double -> Either Error NonNegativeRational
- toDouble :: NonNegativeRational -> Double
- numerator :: NonNegativeRational -> Word64
- denominator :: NonNegativeRational -> Word64
- toRational :: NonNegativeRational -> Rational
- encodeCbor :: NonNegativeRational -> ByteString
- decodeCbor :: ByteString -> Either Error NonNegativeRational
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.
Instances
| Show NonNegativeRational Source # | |
Defined in Cometa.Internal.ProtocolValues Methods showsPrec :: Int -> NonNegativeRational -> ShowS # show :: NonNegativeRational -> String # showList :: [NonNegativeRational] -> ShowS # | |
| Eq NonNegativeRational Source # | |
Defined in Cometa.Internal.ProtocolValues Methods (==) :: NonNegativeRational -> NonNegativeRational -> Bool # (/=) :: NonNegativeRational -> NonNegativeRational -> Bool # | |
| Ord NonNegativeRational Source # | |
Defined in Cometa.Internal.ProtocolValues Methods compare :: NonNegativeRational -> NonNegativeRational -> Ordering # (<) :: NonNegativeRational -> NonNegativeRational -> Bool # (<=) :: NonNegativeRational -> NonNegativeRational -> Bool # (>) :: NonNegativeRational -> NonNegativeRational -> Bool # (>=) :: NonNegativeRational -> NonNegativeRational -> Bool # max :: NonNegativeRational -> NonNegativeRational -> NonNegativeRational # min :: NonNegativeRational -> NonNegativeRational -> NonNegativeRational # | |
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.