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