module Cometa.Protocol.Rational
( NonNegativeRational,
mkNonNegativeRational,
fromDouble,
toDouble,
numerator,
denominator,
toRational,
encodeCbor,
decodeCbor,
)
where
import Cometa.Error (Error (..))
import Cometa.Internal.ProtocolValues (NonNegativeRational, mkNonNegativeRational)
import qualified Cometa.Internal.ProtocolValues as Internal
import Data.ByteString (ByteString)
import Data.Word (Word64)
import Prelude hiding (toRational)
fromDouble :: Double -> Either Error NonNegativeRational
fromDouble :: Double -> Either Error NonNegativeRational
fromDouble Double
value
| Double -> Bool
forall a. RealFloat a => a -> Bool
isNaN Double
value Bool -> Bool -> Bool
|| Double -> Bool
forall a. RealFloat a => a -> Bool
isInfinite Double
value Bool -> Bool -> Bool
|| Double
value Double -> Double -> Bool
forall a. Ord a => a -> a -> Bool
< Double
0 = Error -> Either Error NonNegativeRational
forall a b. a -> Either a b
Left Error
InvalidProtocolDouble
| Integer
whole Integer -> Integer -> Bool
forall a. Ord a => a -> a -> Bool
> Word64 -> Integer
forall a. Integral a => a -> Integer
toInteger (Word64
forall a. Bounded a => a
maxBound :: Word64) = Error -> Either Error NonNegativeRational
forall a b. a -> Either a b
Left Error
InvalidProtocolDouble
| Bool
otherwise =
let fractionalNumerator :: Integer
fractionalNumerator = Double -> Integer
forall b. Integral b => Double -> b
forall a b. (RealFrac a, Integral b) => a -> b
floor ((Double
value Double -> Double -> Double
forall a. Num a => a -> a -> a
- Integer -> Double
forall a. Num a => Integer -> a
fromInteger Integer
whole) Double -> Double -> Double
forall a. Num a => a -> a -> a
* Double
1000000000000000)
fractionalDenominator :: Integer
fractionalDenominator = Integer
1000000000000000
divisor :: Integer
divisor = Integer -> Integer -> Integer
forall a. Integral a => a -> a -> a
gcd Integer
fractionalNumerator Integer
fractionalDenominator
d :: Integer
d = Integer
fractionalDenominator Integer -> Integer -> Integer
forall a. Integral a => a -> a -> a
`quot` Integer
divisor
n :: Integer
n = Integer
fractionalNumerator Integer -> Integer -> Integer
forall a. Integral a => a -> a -> a
`quot` Integer
divisor Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
+ Integer
whole Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
* Integer
d
in Integer -> Integer -> Either Error NonNegativeRational
mkNonNegativeRational Integer
n Integer
d
where
whole :: Integer
whole = Double -> Integer
forall b. Integral b => Double -> b
forall a b. (RealFrac a, Integral b) => a -> b
floor Double
value
toDouble :: NonNegativeRational -> Double
toDouble :: NonNegativeRational -> Double
toDouble NonNegativeRational
value = Word64 -> Double
forall a b. (Integral a, Num b) => a -> b
fromIntegral (NonNegativeRational -> Word64
numerator NonNegativeRational
value) Double -> Double -> Double
forall a. Fractional a => a -> a -> a
/ Word64 -> Double
forall a b. (Integral a, Num b) => a -> b
fromIntegral (NonNegativeRational -> Word64
denominator NonNegativeRational
value)
numerator :: NonNegativeRational -> Word64
numerator :: NonNegativeRational -> Word64
numerator = NonNegativeRational -> Word64
Internal.rationalNumerator
denominator :: NonNegativeRational -> Word64
denominator :: NonNegativeRational -> Word64
denominator = NonNegativeRational -> Word64
Internal.rationalDenominator
toRational :: NonNegativeRational -> Rational
toRational :: NonNegativeRational -> Rational
toRational = NonNegativeRational -> Rational
Internal.rationalValue
encodeCbor :: NonNegativeRational -> ByteString
encodeCbor :: NonNegativeRational -> ByteString
encodeCbor = (NonNegativeRational -> Encoding)
-> NonNegativeRational -> ByteString
forall a. (a -> Encoding) -> a -> ByteString
Internal.encodeBytes NonNegativeRational -> Encoding
Internal.encodeRational
decodeCbor :: ByteString -> Either Error NonNegativeRational
decodeCbor :: ByteString -> Either Error NonNegativeRational
decodeCbor = (forall s. Decoder s NonNegativeRational)
-> ByteString -> Either Error NonNegativeRational
forall a. (forall s. Decoder s a) -> ByteString -> Either Error a
Internal.decodeBytes Decoder s NonNegativeRational
forall s. Decoder s NonNegativeRational
Internal.decodeRational