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

Cometa.UnitInterval

Description

Exact proportions from zero through one, inclusive. Pairs retain their numerator and denominator; comparison is numeric.

mkUnitInterval 2 3 validates a two-thirds voting threshold.

Synopsis

Documentation

data UnitInterval Source #

Exact proportion between zero and one, inclusive. Equality and ordering compare numeric values; the original pair is retained for encoding.

Instances

Instances details
Show UnitInterval Source # 
Instance details

Defined in Cometa.Internal.ProtocolValues

Eq UnitInterval Source # 
Instance details

Defined in Cometa.Internal.ProtocolValues

Ord UnitInterval Source # 
Instance details

Defined in Cometa.Internal.ProtocolValues

mkUnitInterval :: Integer -> Integer -> Either Error UnitInterval Source #

Validate a proportion numerator and denominator. Invalid unsigned bounds or a zero denominator return InvalidProtocolRational; a numerator larger than its denominator returns InvalidUnitInterval.

fromDouble :: Double -> Either Error UnitInterval Source #

Convert a finite number in [0,1] using the native decimal-floor rule: reduce floor(value * 10^15) / 10^15. This may round down and is not the exact binary rational representation of the Double. NaN, infinities and values outside the interval return InvalidUnitIntervalDouble. Negative zero is zero.

toDouble :: UnitInterval -> Double Source #

Approximate the fraction by converting its numerator and denominator separately to Double and dividing, matching the native conversion. This can differ from rounding an exact Rational once.

numerator :: UnitInterval -> Word64 Source #

Original unsigned numerator.

denominator :: UnitInterval -> Word64 Source #

Original positive unsigned denominator.

toRational :: UnitInterval -> Rational Source #

Exact numeric proportion.

asNonNegativeRational :: UnitInterval -> NonNegativeRational Source #

Forget the upper bound while retaining the original fraction pair.

encodeCbor :: UnitInterval -> ByteString Source #

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

decodeCbor :: ByteString -> Either Error UnitInterval 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.