mirror of
https://github.com/tfausak/witch.git
synced 2024-11-22 14:58:13 +03:00
Add instance for converting from rational into fixed
This commit is contained in:
parent
09888d81a2
commit
aac3a0e755
@ -1,6 +1,7 @@
|
|||||||
{-# OPTIONS_GHC -Wno-orphans #-}
|
{-# OPTIONS_GHC -Wno-orphans #-}
|
||||||
{-# LANGUAGE FlexibleInstances #-}
|
{-# LANGUAGE FlexibleInstances #-}
|
||||||
{-# LANGUAGE MultiParamTypeClasses #-}
|
{-# LANGUAGE MultiParamTypeClasses #-}
|
||||||
|
{-# LANGUAGE ScopedTypeVariables #-}
|
||||||
{-# LANGUAGE TypeApplications #-}
|
{-# LANGUAGE TypeApplications #-}
|
||||||
|
|
||||||
module Witch.Instances where
|
module Witch.Instances where
|
||||||
@ -907,6 +908,14 @@ instance From.From Rational Float where
|
|||||||
instance From.From Rational Double where
|
instance From.From Rational Double where
|
||||||
from = fromRational
|
from = fromRational
|
||||||
|
|
||||||
|
-- | TODO
|
||||||
|
instance Fixed.HasResolution a => TryFrom.TryFrom Rational (Fixed.Fixed a) where
|
||||||
|
tryFrom = Utility.eitherTryFrom $ \s ->
|
||||||
|
let t = fromRational s :: Fixed.Fixed a
|
||||||
|
in if toRational t == s
|
||||||
|
then Right t
|
||||||
|
else Left Exception.LossOfPrecision
|
||||||
|
|
||||||
-- Fixed
|
-- Fixed
|
||||||
|
|
||||||
-- | Uses 'Fixed.MkFixed'. This means @from \@Integer \@Centi 2@ is @0.02@
|
-- | Uses 'Fixed.MkFixed'. This means @from \@Integer \@Centi 2@ is @0.02@
|
||||||
|
@ -1448,6 +1448,15 @@ main = runTestTTAndExit $ "Witch" ~:
|
|||||||
, f 0.5 ~?= 0.5
|
, f 0.5 ~?= 0.5
|
||||||
, f (-0.5) ~?= (-0.5)
|
, f (-0.5) ~?= (-0.5)
|
||||||
]
|
]
|
||||||
|
, "TryFrom Rational (Fixed a)" ~:
|
||||||
|
let f = hush . Witch.tryFrom @Rational @Fixed.Deci in
|
||||||
|
[ hush (Witch.tryFrom @Rational @Fixed.Uni 1) ~?= Just 1
|
||||||
|
, hush (Witch.tryFrom @Rational @Fixed.Uni 1.2) ~?= Nothing
|
||||||
|
, f 0.1 ~?= Just 0.1
|
||||||
|
, f 1.2 ~?= Just 1.2
|
||||||
|
, f 12.3 ~?= Just 12.3
|
||||||
|
, f 0.12 ~?= Nothing
|
||||||
|
]
|
||||||
|
|
||||||
-- Fixed
|
-- Fixed
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user