{-# LANGUAGE DeriveDataTypeable #-}
module Data.RealNumber.Rational(RealNumber(..)
, AsFixed(..), asFixed
, toFixed, fromFixed
, Nat
) where
import Data.Aeson
import Data.Data
import Data.Fixed
import Data.Hashable
import Data.List (dropWhileEnd)
import GHC.Generics (Generic (..))
import GHC.TypeLits
import Test.QuickCheck (Arbitrary (..))
import Control.Monad.Random
import Data.Ratio
import Control.DeepSeq
newtype RealNumber (p :: Nat) = RealNumber Rational
deriving (RealNumber p -> RealNumber p -> Bool
(RealNumber p -> RealNumber p -> Bool)
-> (RealNumber p -> RealNumber p -> Bool) -> Eq (RealNumber p)
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
forall (p :: Nat). RealNumber p -> RealNumber p -> Bool
/= :: RealNumber p -> RealNumber p -> Bool
$c/= :: forall (p :: Nat). RealNumber p -> RealNumber p -> Bool
== :: RealNumber p -> RealNumber p -> Bool
$c== :: forall (p :: Nat). RealNumber p -> RealNumber p -> Bool
Eq,Eq (RealNumber p)
Eq (RealNumber p)
-> (RealNumber p -> RealNumber p -> Ordering)
-> (RealNumber p -> RealNumber p -> Bool)
-> (RealNumber p -> RealNumber p -> Bool)
-> (RealNumber p -> RealNumber p -> Bool)
-> (RealNumber p -> RealNumber p -> Bool)
-> (RealNumber p -> RealNumber p -> RealNumber p)
-> (RealNumber p -> RealNumber p -> RealNumber p)
-> Ord (RealNumber p)
RealNumber p -> RealNumber p -> Bool
RealNumber p -> RealNumber p -> Ordering
RealNumber p -> RealNumber p -> RealNumber p
forall a.
Eq a
-> (a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
forall (p :: Nat). Eq (RealNumber p)
forall (p :: Nat). RealNumber p -> RealNumber p -> Bool
forall (p :: Nat). RealNumber p -> RealNumber p -> Ordering
forall (p :: Nat). RealNumber p -> RealNumber p -> RealNumber p
min :: RealNumber p -> RealNumber p -> RealNumber p
$cmin :: forall (p :: Nat). RealNumber p -> RealNumber p -> RealNumber p
max :: RealNumber p -> RealNumber p -> RealNumber p
$cmax :: forall (p :: Nat). RealNumber p -> RealNumber p -> RealNumber p
>= :: RealNumber p -> RealNumber p -> Bool
$c>= :: forall (p :: Nat). RealNumber p -> RealNumber p -> Bool
> :: RealNumber p -> RealNumber p -> Bool
$c> :: forall (p :: Nat). RealNumber p -> RealNumber p -> Bool
<= :: RealNumber p -> RealNumber p -> Bool
$c<= :: forall (p :: Nat). RealNumber p -> RealNumber p -> Bool
< :: RealNumber p -> RealNumber p -> Bool
$c< :: forall (p :: Nat). RealNumber p -> RealNumber p -> Bool
compare :: RealNumber p -> RealNumber p -> Ordering
$ccompare :: forall (p :: Nat). RealNumber p -> RealNumber p -> Ordering
$cp1Ord :: forall (p :: Nat). Eq (RealNumber p)
Ord,Typeable (RealNumber p)
DataType
Constr
Typeable (RealNumber p)
-> (forall (c :: * -> *).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> RealNumber p -> c (RealNumber p))
-> (forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c (RealNumber p))
-> (RealNumber p -> Constr)
-> (RealNumber p -> DataType)
-> (forall (t :: * -> *) (c :: * -> *).
Typeable t =>
(forall d. Data d => c (t d)) -> Maybe (c (RealNumber p)))
-> (forall (t :: * -> * -> *) (c :: * -> *).
Typeable t =>
(forall d e. (Data d, Data e) => c (t d e))
-> Maybe (c (RealNumber p)))
-> ((forall b. Data b => b -> b) -> RealNumber p -> RealNumber p)
-> (forall r r'.
(r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> RealNumber p -> r)
-> (forall r r'.
(r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> RealNumber p -> r)
-> (forall u. (forall d. Data d => d -> u) -> RealNumber p -> [u])
-> (forall u.
Int -> (forall d. Data d => d -> u) -> RealNumber p -> u)
-> (forall (m :: * -> *).
Monad m =>
(forall d. Data d => d -> m d) -> RealNumber p -> m (RealNumber p))
-> (forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> RealNumber p -> m (RealNumber p))
-> (forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> RealNumber p -> m (RealNumber p))
-> Data (RealNumber p)
RealNumber p -> DataType
RealNumber p -> Constr
(forall b. Data b => b -> b) -> RealNumber p -> RealNumber p
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> RealNumber p -> c (RealNumber p)
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c (RealNumber p)
forall a.
Typeable a
-> (forall (c :: * -> *).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> a -> c a)
-> (forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c a)
-> (a -> Constr)
-> (a -> DataType)
-> (forall (t :: * -> *) (c :: * -> *).
Typeable t =>
(forall d. Data d => c (t d)) -> Maybe (c a))
-> (forall (t :: * -> * -> *) (c :: * -> *).
Typeable t =>
(forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c a))
-> ((forall b. Data b => b -> b) -> a -> a)
-> (forall r r'.
(r -> r' -> r) -> r -> (forall d. Data d => d -> r') -> a -> r)
-> (forall r r'.
(r' -> r -> r) -> r -> (forall d. Data d => d -> r') -> a -> r)
-> (forall u. (forall d. Data d => d -> u) -> a -> [u])
-> (forall u. Int -> (forall d. Data d => d -> u) -> a -> u)
-> (forall (m :: * -> *).
Monad m =>
(forall d. Data d => d -> m d) -> a -> m a)
-> (forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> a -> m a)
-> (forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> a -> m a)
-> Data a
forall u. Int -> (forall d. Data d => d -> u) -> RealNumber p -> u
forall u. (forall d. Data d => d -> u) -> RealNumber p -> [u]
forall r r'.
(r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> RealNumber p -> r
forall r r'.
(r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> RealNumber p -> r
forall (p :: Nat). KnownNat p => Typeable (RealNumber p)
forall (p :: Nat). KnownNat p => RealNumber p -> DataType
forall (p :: Nat). KnownNat p => RealNumber p -> Constr
forall (p :: Nat).
KnownNat p =>
(forall b. Data b => b -> b) -> RealNumber p -> RealNumber p
forall (p :: Nat) u.
KnownNat p =>
Int -> (forall d. Data d => d -> u) -> RealNumber p -> u
forall (p :: Nat) u.
KnownNat p =>
(forall d. Data d => d -> u) -> RealNumber p -> [u]
forall (p :: Nat) r r'.
KnownNat p =>
(r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> RealNumber p -> r
forall (p :: Nat) r r'.
KnownNat p =>
(r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> RealNumber p -> r
forall (p :: Nat) (m :: * -> *).
(KnownNat p, Monad m) =>
(forall d. Data d => d -> m d) -> RealNumber p -> m (RealNumber p)
forall (p :: Nat) (m :: * -> *).
(KnownNat p, MonadPlus m) =>
(forall d. Data d => d -> m d) -> RealNumber p -> m (RealNumber p)
forall (p :: Nat) (c :: * -> *).
KnownNat p =>
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c (RealNumber p)
forall (p :: Nat) (c :: * -> *).
KnownNat p =>
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> RealNumber p -> c (RealNumber p)
forall (p :: Nat) (t :: * -> *) (c :: * -> *).
(KnownNat p, Typeable t) =>
(forall d. Data d => c (t d)) -> Maybe (c (RealNumber p))
forall (p :: Nat) (t :: * -> * -> *) (c :: * -> *).
(KnownNat p, Typeable t) =>
(forall d e. (Data d, Data e) => c (t d e))
-> Maybe (c (RealNumber p))
forall (m :: * -> *).
Monad m =>
(forall d. Data d => d -> m d) -> RealNumber p -> m (RealNumber p)
forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> RealNumber p -> m (RealNumber p)
forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c (RealNumber p)
forall (c :: * -> *).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> RealNumber p -> c (RealNumber p)
forall (t :: * -> *) (c :: * -> *).
Typeable t =>
(forall d. Data d => c (t d)) -> Maybe (c (RealNumber p))
forall (t :: * -> * -> *) (c :: * -> *).
Typeable t =>
(forall d e. (Data d, Data e) => c (t d e))
-> Maybe (c (RealNumber p))
$cRealNumber :: Constr
$tRealNumber :: DataType
gmapMo :: (forall d. Data d => d -> m d) -> RealNumber p -> m (RealNumber p)
$cgmapMo :: forall (p :: Nat) (m :: * -> *).
(KnownNat p, MonadPlus m) =>
(forall d. Data d => d -> m d) -> RealNumber p -> m (RealNumber p)
gmapMp :: (forall d. Data d => d -> m d) -> RealNumber p -> m (RealNumber p)
$cgmapMp :: forall (p :: Nat) (m :: * -> *).
(KnownNat p, MonadPlus m) =>
(forall d. Data d => d -> m d) -> RealNumber p -> m (RealNumber p)
gmapM :: (forall d. Data d => d -> m d) -> RealNumber p -> m (RealNumber p)
$cgmapM :: forall (p :: Nat) (m :: * -> *).
(KnownNat p, Monad m) =>
(forall d. Data d => d -> m d) -> RealNumber p -> m (RealNumber p)
gmapQi :: Int -> (forall d. Data d => d -> u) -> RealNumber p -> u
$cgmapQi :: forall (p :: Nat) u.
KnownNat p =>
Int -> (forall d. Data d => d -> u) -> RealNumber p -> u
gmapQ :: (forall d. Data d => d -> u) -> RealNumber p -> [u]
$cgmapQ :: forall (p :: Nat) u.
KnownNat p =>
(forall d. Data d => d -> u) -> RealNumber p -> [u]
gmapQr :: (r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> RealNumber p -> r
$cgmapQr :: forall (p :: Nat) r r'.
KnownNat p =>
(r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> RealNumber p -> r
gmapQl :: (r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> RealNumber p -> r
$cgmapQl :: forall (p :: Nat) r r'.
KnownNat p =>
(r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> RealNumber p -> r
gmapT :: (forall b. Data b => b -> b) -> RealNumber p -> RealNumber p
$cgmapT :: forall (p :: Nat).
KnownNat p =>
(forall b. Data b => b -> b) -> RealNumber p -> RealNumber p
dataCast2 :: (forall d e. (Data d, Data e) => c (t d e))
-> Maybe (c (RealNumber p))
$cdataCast2 :: forall (p :: Nat) (t :: * -> * -> *) (c :: * -> *).
(KnownNat p, Typeable t) =>
(forall d e. (Data d, Data e) => c (t d e))
-> Maybe (c (RealNumber p))
dataCast1 :: (forall d. Data d => c (t d)) -> Maybe (c (RealNumber p))
$cdataCast1 :: forall (p :: Nat) (t :: * -> *) (c :: * -> *).
(KnownNat p, Typeable t) =>
(forall d. Data d => c (t d)) -> Maybe (c (RealNumber p))
dataTypeOf :: RealNumber p -> DataType
$cdataTypeOf :: forall (p :: Nat). KnownNat p => RealNumber p -> DataType
toConstr :: RealNumber p -> Constr
$ctoConstr :: forall (p :: Nat). KnownNat p => RealNumber p -> Constr
gunfold :: (forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c (RealNumber p)
$cgunfold :: forall (p :: Nat) (c :: * -> *).
KnownNat p =>
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c (RealNumber p)
gfoldl :: (forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> RealNumber p -> c (RealNumber p)
$cgfoldl :: forall (p :: Nat) (c :: * -> *).
KnownNat p =>
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> RealNumber p -> c (RealNumber p)
$cp1Data :: forall (p :: Nat). KnownNat p => Typeable (RealNumber p)
Data,Integer -> RealNumber p
RealNumber p -> RealNumber p
RealNumber p -> RealNumber p -> RealNumber p
(RealNumber p -> RealNumber p -> RealNumber p)
-> (RealNumber p -> RealNumber p -> RealNumber p)
-> (RealNumber p -> RealNumber p -> RealNumber p)
-> (RealNumber p -> RealNumber p)
-> (RealNumber p -> RealNumber p)
-> (RealNumber p -> RealNumber p)
-> (Integer -> RealNumber p)
-> Num (RealNumber p)
forall a.
(a -> a -> a)
-> (a -> a -> a)
-> (a -> a -> a)
-> (a -> a)
-> (a -> a)
-> (a -> a)
-> (Integer -> a)
-> Num a
forall (p :: Nat). Integer -> RealNumber p
forall (p :: Nat). RealNumber p -> RealNumber p
forall (p :: Nat). RealNumber p -> RealNumber p -> RealNumber p
fromInteger :: Integer -> RealNumber p
$cfromInteger :: forall (p :: Nat). Integer -> RealNumber p
signum :: RealNumber p -> RealNumber p
$csignum :: forall (p :: Nat). RealNumber p -> RealNumber p
abs :: RealNumber p -> RealNumber p
$cabs :: forall (p :: Nat). RealNumber p -> RealNumber p
negate :: RealNumber p -> RealNumber p
$cnegate :: forall (p :: Nat). RealNumber p -> RealNumber p
* :: RealNumber p -> RealNumber p -> RealNumber p
$c* :: forall (p :: Nat). RealNumber p -> RealNumber p -> RealNumber p
- :: RealNumber p -> RealNumber p -> RealNumber p
$c- :: forall (p :: Nat). RealNumber p -> RealNumber p -> RealNumber p
+ :: RealNumber p -> RealNumber p -> RealNumber p
$c+ :: forall (p :: Nat). RealNumber p -> RealNumber p -> RealNumber p
Num,Num (RealNumber p)
Num (RealNumber p)
-> (RealNumber p -> RealNumber p -> RealNumber p)
-> (RealNumber p -> RealNumber p)
-> (Rational -> RealNumber p)
-> Fractional (RealNumber p)
Rational -> RealNumber p
RealNumber p -> RealNumber p
RealNumber p -> RealNumber p -> RealNumber p
forall a.
Num a
-> (a -> a -> a) -> (a -> a) -> (Rational -> a) -> Fractional a
forall (p :: Nat). Num (RealNumber p)
forall (p :: Nat). Rational -> RealNumber p
forall (p :: Nat). RealNumber p -> RealNumber p
forall (p :: Nat). RealNumber p -> RealNumber p -> RealNumber p
fromRational :: Rational -> RealNumber p
$cfromRational :: forall (p :: Nat). Rational -> RealNumber p
recip :: RealNumber p -> RealNumber p
$crecip :: forall (p :: Nat). RealNumber p -> RealNumber p
/ :: RealNumber p -> RealNumber p -> RealNumber p
$c/ :: forall (p :: Nat). RealNumber p -> RealNumber p -> RealNumber p
$cp1Fractional :: forall (p :: Nat). Num (RealNumber p)
Fractional,Num (RealNumber p)
Ord (RealNumber p)
Num (RealNumber p)
-> Ord (RealNumber p)
-> (RealNumber p -> Rational)
-> Real (RealNumber p)
RealNumber p -> Rational
forall a. Num a -> Ord a -> (a -> Rational) -> Real a
forall (p :: Nat). Num (RealNumber p)
forall (p :: Nat). Ord (RealNumber p)
forall (p :: Nat). RealNumber p -> Rational
toRational :: RealNumber p -> Rational
$ctoRational :: forall (p :: Nat). RealNumber p -> Rational
$cp2Real :: forall (p :: Nat). Ord (RealNumber p)
$cp1Real :: forall (p :: Nat). Num (RealNumber p)
Real,Fractional (RealNumber p)
Real (RealNumber p)
Real (RealNumber p)
-> Fractional (RealNumber p)
-> (forall b. Integral b => RealNumber p -> (b, RealNumber p))
-> (forall b. Integral b => RealNumber p -> b)
-> (forall b. Integral b => RealNumber p -> b)
-> (forall b. Integral b => RealNumber p -> b)
-> (forall b. Integral b => RealNumber p -> b)
-> RealFrac (RealNumber p)
RealNumber p -> b
RealNumber p -> b
RealNumber p -> b
RealNumber p -> b
RealNumber p -> (b, RealNumber p)
forall b. Integral b => RealNumber p -> b
forall b. Integral b => RealNumber p -> (b, RealNumber p)
forall a.
Real a
-> Fractional a
-> (forall b. Integral b => a -> (b, a))
-> (forall b. Integral b => a -> b)
-> (forall b. Integral b => a -> b)
-> (forall b. Integral b => a -> b)
-> (forall b. Integral b => a -> b)
-> RealFrac a
forall (p :: Nat). Fractional (RealNumber p)
forall (p :: Nat). Real (RealNumber p)
forall (p :: Nat) b. Integral b => RealNumber p -> b
forall (p :: Nat) b.
Integral b =>
RealNumber p -> (b, RealNumber p)
floor :: RealNumber p -> b
$cfloor :: forall (p :: Nat) b. Integral b => RealNumber p -> b
ceiling :: RealNumber p -> b
$cceiling :: forall (p :: Nat) b. Integral b => RealNumber p -> b
round :: RealNumber p -> b
$cround :: forall (p :: Nat) b. Integral b => RealNumber p -> b
truncate :: RealNumber p -> b
$ctruncate :: forall (p :: Nat) b. Integral b => RealNumber p -> b
properFraction :: RealNumber p -> (b, RealNumber p)
$cproperFraction :: forall (p :: Nat) b.
Integral b =>
RealNumber p -> (b, RealNumber p)
$cp2RealFrac :: forall (p :: Nat). Fractional (RealNumber p)
$cp1RealFrac :: forall (p :: Nat). Real (RealNumber p)
RealFrac,(forall x. RealNumber p -> Rep (RealNumber p) x)
-> (forall x. Rep (RealNumber p) x -> RealNumber p)
-> Generic (RealNumber p)
forall x. Rep (RealNumber p) x -> RealNumber p
forall x. RealNumber p -> Rep (RealNumber p) x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
forall (p :: Nat) x. Rep (RealNumber p) x -> RealNumber p
forall (p :: Nat) x. RealNumber p -> Rep (RealNumber p) x
$cto :: forall (p :: Nat) x. Rep (RealNumber p) x -> RealNumber p
$cfrom :: forall (p :: Nat) x. RealNumber p -> Rep (RealNumber p) x
Generic,Int -> RealNumber p -> Int
RealNumber p -> Int
(Int -> RealNumber p -> Int)
-> (RealNumber p -> Int) -> Hashable (RealNumber p)
forall a. (Int -> a -> Int) -> (a -> Int) -> Hashable a
forall (p :: Nat). Int -> RealNumber p -> Int
forall (p :: Nat). RealNumber p -> Int
hash :: RealNumber p -> Int
$chash :: forall (p :: Nat). RealNumber p -> Int
hashWithSalt :: Int -> RealNumber p -> Int
$chashWithSalt :: forall (p :: Nat). Int -> RealNumber p -> Int
Hashable,[RealNumber p] -> Encoding
[RealNumber p] -> Value
RealNumber p -> Encoding
RealNumber p -> Value
(RealNumber p -> Value)
-> (RealNumber p -> Encoding)
-> ([RealNumber p] -> Value)
-> ([RealNumber p] -> Encoding)
-> ToJSON (RealNumber p)
forall a.
(a -> Value)
-> (a -> Encoding)
-> ([a] -> Value)
-> ([a] -> Encoding)
-> ToJSON a
forall (p :: Nat). [RealNumber p] -> Encoding
forall (p :: Nat). [RealNumber p] -> Value
forall (p :: Nat). RealNumber p -> Encoding
forall (p :: Nat). RealNumber p -> Value
toEncodingList :: [RealNumber p] -> Encoding
$ctoEncodingList :: forall (p :: Nat). [RealNumber p] -> Encoding
toJSONList :: [RealNumber p] -> Value
$ctoJSONList :: forall (p :: Nat). [RealNumber p] -> Value
toEncoding :: RealNumber p -> Encoding
$ctoEncoding :: forall (p :: Nat). RealNumber p -> Encoding
toJSON :: RealNumber p -> Value
$ctoJSON :: forall (p :: Nat). RealNumber p -> Value
ToJSON,Value -> Parser [RealNumber p]
Value -> Parser (RealNumber p)
(Value -> Parser (RealNumber p))
-> (Value -> Parser [RealNumber p]) -> FromJSON (RealNumber p)
forall a.
(Value -> Parser a) -> (Value -> Parser [a]) -> FromJSON a
forall (p :: Nat). Value -> Parser [RealNumber p]
forall (p :: Nat). Value -> Parser (RealNumber p)
parseJSONList :: Value -> Parser [RealNumber p]
$cparseJSONList :: forall (p :: Nat). Value -> Parser [RealNumber p]
parseJSON :: Value -> Parser (RealNumber p)
$cparseJSON :: forall (p :: Nat). Value -> Parser (RealNumber p)
FromJSON,RealNumber p -> ()
(RealNumber p -> ()) -> NFData (RealNumber p)
forall a. (a -> ()) -> NFData a
forall (p :: Nat). RealNumber p -> ()
rnf :: RealNumber p -> ()
$crnf :: forall (p :: Nat). RealNumber p -> ()
NFData)
data NatPrec (p :: Nat) = NatPrec
instance KnownNat p => HasResolution (NatPrec p) where
resolution :: p (NatPrec p) -> Integer
resolution p (NatPrec p)
_ = Integer
10 Integer -> Integer -> Integer
forall a b. (Num a, Integral b) => a -> b -> a
^ (NatPrec p -> Integer
forall (n :: Nat) (proxy :: Nat -> *).
KnownNat n =>
proxy n -> Integer
natVal (NatPrec p
forall (p :: Nat). NatPrec p
NatPrec @p))
instance KnownNat p => Show (RealNumber p) where
showsPrec :: Int -> RealNumber p -> ShowS
showsPrec Int
d RealNumber p
r = Bool -> ShowS -> ShowS
showParen (Int
d Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> Int
app_prec Bool -> Bool -> Bool
&& RealNumber p
r RealNumber p -> RealNumber p -> Bool
forall a. Ord a => a -> a -> Bool
< RealNumber p
0) (ShowS -> ShowS) -> ShowS -> ShowS
forall a b. (a -> b) -> a -> b
$
case RealNumber p -> AsFixed (NatPrec p)
forall (p :: Nat).
KnownNat p =>
RealNumber p -> AsFixed (NatPrec p)
asFixed RealNumber p
r of
Exact Fixed (NatPrec p)
p -> String -> ShowS
showString ((Char -> Bool) -> ShowS
forall a. (a -> Bool) -> [a] -> [a]
dropWhileEnd (Char -> Char -> Bool
forall a. Eq a => a -> a -> Bool
== Char
'.') ShowS
-> (Fixed (NatPrec p) -> String) -> Fixed (NatPrec p) -> String
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Char -> Bool) -> ShowS
forall a. (a -> Bool) -> [a] -> [a]
dropWhileEnd (Char -> Char -> Bool
forall a. Eq a => a -> a -> Bool
== Char
'0') ShowS
-> (Fixed (NatPrec p) -> String) -> Fixed (NatPrec p) -> String
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Fixed (NatPrec p) -> String
forall a. Show a => a -> String
show (Fixed (NatPrec p) -> String) -> Fixed (NatPrec p) -> String
forall a b. (a -> b) -> a -> b
$ Fixed (NatPrec p)
p)
Lossy Fixed (NatPrec p)
p -> Fixed (NatPrec p) -> ShowS
forall a. Show a => a -> ShowS
shows Fixed (NatPrec p)
p ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Char -> ShowS
showChar Char
'~'
where
app_prec :: Int
app_prec = Int
10
instance KnownNat p => Read (RealNumber p) where
readsPrec :: Int -> ReadS (RealNumber p)
readsPrec Int
i = ((Fixed (NatPrec p), String) -> (RealNumber p, String))
-> [(Fixed (NatPrec p), String)] -> [(RealNumber p, String)]
forall a b. (a -> b) -> [a] -> [b]
map (Fixed (NatPrec p), String) -> (RealNumber p, String)
forall a (p :: Nat).
Real a =>
(a, String) -> (RealNumber p, String)
wrap ([(Fixed (NatPrec p), String)] -> [(RealNumber p, String)])
-> (String -> [(Fixed (NatPrec p), String)])
-> ReadS (RealNumber p)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> String -> [(Fixed (NatPrec p), String)]
forall a. Read a => Int -> ReadS a
readsPrec @(Fixed (NatPrec p)) Int
i
where
wrap :: (a, String) -> (RealNumber p, String)
wrap (Rational -> RealNumber p
forall (p :: Nat). Rational -> RealNumber p
RealNumber (Rational -> RealNumber p) -> (a -> Rational) -> a -> RealNumber p
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> Rational
forall a b. (Real a, Fractional b) => a -> b
realToFrac -> RealNumber p
x,String
s') = case String
s' of
Char
'~':String
s'' -> (RealNumber p
x,String
s'')
String
_ -> (RealNumber p
x,String
s')
instance KnownNat p => Arbitrary (RealNumber p) where
arbitrary :: Gen (RealNumber p)
arbitrary = Fixed (NatPrec p) -> RealNumber p
forall (p :: Nat). KnownNat p => Fixed (NatPrec p) -> RealNumber p
fromFixed (Fixed (NatPrec p) -> RealNumber p)
-> Gen (Fixed (NatPrec p)) -> Gen (RealNumber p)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Gen (Fixed (NatPrec p))
forall a. Arbitrary a => Gen a
arbitrary
instance Random (RealNumber p) where
randomR :: (RealNumber p, RealNumber p) -> g -> (RealNumber p, g)
randomR (RealNumber p
a,RealNumber p
b) = Rand g (RealNumber p) -> g -> (RealNumber p, g)
forall g a. Rand g a -> g -> (a, g)
runRand (Rand g (RealNumber p) -> g -> (RealNumber p, g))
-> Rand g (RealNumber p) -> g -> (RealNumber p, g)
forall a b. (a -> b) -> a -> b
$ do
RealNumber p
v <- Rand g (RealNumber p)
forall (m :: * -> *) a. (MonadRandom m, Random a) => m a
getRandom
RealNumber p -> Rand g (RealNumber p)
forall (f :: * -> *) a. Applicative f => a -> f a
pure (RealNumber p -> Rand g (RealNumber p))
-> RealNumber p -> Rand g (RealNumber p)
forall a b. (a -> b) -> a -> b
$ (RealNumber p
bRealNumber p -> RealNumber p -> RealNumber p
forall a. Num a => a -> a -> a
-RealNumber p
a)RealNumber p -> RealNumber p -> RealNumber p
forall a. Num a => a -> a -> a
*RealNumber p -> RealNumber p
forall a. Num a => a -> a
abs RealNumber p
v RealNumber p -> RealNumber p -> RealNumber p
forall a. Num a => a -> a -> a
+ RealNumber p
a
random :: g -> (RealNumber p, g)
random = Rand g (RealNumber p) -> g -> (RealNumber p, g)
forall g a. Rand g a -> g -> (a, g)
runRand (Rand g (RealNumber p) -> g -> (RealNumber p, g))
-> Rand g (RealNumber p) -> g -> (RealNumber p, g)
forall a b. (a -> b) -> a -> b
$ do
Int
v <- RandT g Identity Int
forall (m :: * -> *) a. (MonadRandom m, Random a) => m a
getRandom
let fromInt :: Int -> Integer; fromInt :: Int -> Integer
fromInt = Int -> Integer
forall a b. (Integral a, Num b) => a -> b
fromIntegral
RealNumber p -> Rand g (RealNumber p)
forall (f :: * -> *) a. Applicative f => a -> f a
pure (RealNumber p -> Rand g (RealNumber p))
-> RealNumber p -> Rand g (RealNumber p)
forall a b. (a -> b) -> a -> b
$ Rational -> RealNumber p
forall (p :: Nat). Rational -> RealNumber p
RealNumber (Rational -> RealNumber p) -> Rational -> RealNumber p
forall a b. (a -> b) -> a -> b
$ Int -> Integer
fromInt Int
v Integer -> Integer -> Rational
forall a. Integral a => a -> a -> Ratio a
% Int -> Integer
fromInt Int
forall a. Bounded a => a
maxBound
data AsFixed p = Exact !(Fixed p) | Lossy !(Fixed p) deriving (Int -> AsFixed p -> ShowS
[AsFixed p] -> ShowS
AsFixed p -> String
(Int -> AsFixed p -> ShowS)
-> (AsFixed p -> String)
-> ([AsFixed p] -> ShowS)
-> Show (AsFixed p)
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
forall k (p :: k). HasResolution p => Int -> AsFixed p -> ShowS
forall k (p :: k). HasResolution p => [AsFixed p] -> ShowS
forall k (p :: k). HasResolution p => AsFixed p -> String
showList :: [AsFixed p] -> ShowS
$cshowList :: forall k (p :: k). HasResolution p => [AsFixed p] -> ShowS
show :: AsFixed p -> String
$cshow :: forall k (p :: k). HasResolution p => AsFixed p -> String
showsPrec :: Int -> AsFixed p -> ShowS
$cshowsPrec :: forall k (p :: k). HasResolution p => Int -> AsFixed p -> ShowS
Show,AsFixed p -> AsFixed p -> Bool
(AsFixed p -> AsFixed p -> Bool)
-> (AsFixed p -> AsFixed p -> Bool) -> Eq (AsFixed p)
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
forall k (p :: k). AsFixed p -> AsFixed p -> Bool
/= :: AsFixed p -> AsFixed p -> Bool
$c/= :: forall k (p :: k). AsFixed p -> AsFixed p -> Bool
== :: AsFixed p -> AsFixed p -> Bool
$c== :: forall k (p :: k). AsFixed p -> AsFixed p -> Bool
Eq)
toFixed :: KnownNat p => RealNumber p -> Fixed (NatPrec p)
toFixed :: RealNumber p -> Fixed (NatPrec p)
toFixed = RealNumber p -> Fixed (NatPrec p)
forall a b. (Real a, Fractional b) => a -> b
realToFrac
fromFixed :: KnownNat p => Fixed (NatPrec p) -> RealNumber p
fromFixed :: Fixed (NatPrec p) -> RealNumber p
fromFixed = Fixed (NatPrec p) -> RealNumber p
forall a b. (Real a, Fractional b) => a -> b
realToFrac
asFixed :: KnownNat p => RealNumber p -> AsFixed (NatPrec p)
asFixed :: RealNumber p -> AsFixed (NatPrec p)
asFixed RealNumber p
r = let p :: Fixed (NatPrec p)
p = RealNumber p -> Fixed (NatPrec p)
forall (p :: Nat). KnownNat p => RealNumber p -> Fixed (NatPrec p)
toFixed RealNumber p
r in if RealNumber p
r RealNumber p -> RealNumber p -> Bool
forall a. Eq a => a -> a -> Bool
== Fixed (NatPrec p) -> RealNumber p
forall (p :: Nat). KnownNat p => Fixed (NatPrec p) -> RealNumber p
fromFixed Fixed (NatPrec p)
p then Fixed (NatPrec p) -> AsFixed (NatPrec p)
forall k (p :: k). Fixed p -> AsFixed p
Exact Fixed (NatPrec p)
p else Fixed (NatPrec p) -> AsFixed (NatPrec p)
forall k (p :: k). Fixed p -> AsFixed p
Lossy Fixed (NatPrec p)
p