{-# LANGUAGE DeriveDataTypeable #-}
module HGeometry.Number.Real.Rational(
RealNumber(..)
, AsFixed(..), asFixed
, toFixed, fromFixed
, Nat
) where
import Control.DeepSeq
import Control.Monad.Random
import Data.Aeson
import Data.Data
import Data.Fixed
import Data.List (dropWhileEnd)
import Data.Ratio
import GHC.Generics (Generic (..))
import GHC.TypeLits
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 (p :: Nat). RealNumber p -> RealNumber p -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$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
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 (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
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
$ccompare :: forall (p :: Nat). RealNumber p -> RealNumber p -> Ordering
compare :: RealNumber p -> RealNumber p -> Ordering
$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
>= :: RealNumber p -> RealNumber p -> Bool
$cmax :: forall (p :: Nat). RealNumber p -> RealNumber p -> RealNumber p
max :: RealNumber p -> RealNumber p -> RealNumber p
$cmin :: forall (p :: Nat). RealNumber p -> RealNumber p -> RealNumber p
min :: RealNumber p -> RealNumber p -> RealNumber p
Ord,Typeable (RealNumber p)
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 -> Constr
RealNumber p -> DataType
(forall b. Data b => b -> b) -> RealNumber p -> RealNumber p
forall (p :: Nat). KnownNat p => Typeable (RealNumber p)
forall (p :: Nat). KnownNat p => RealNumber p -> Constr
forall (p :: Nat). KnownNat p => RealNumber p -> DataType
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 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 (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))
$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)
gfoldl :: forall (c :: * -> *).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> RealNumber p -> 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)
gunfold :: forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c (RealNumber p)
$ctoConstr :: forall (p :: Nat). KnownNat p => RealNumber p -> Constr
toConstr :: RealNumber p -> Constr
$cdataTypeOf :: forall (p :: Nat). KnownNat p => RealNumber p -> DataType
dataTypeOf :: RealNumber p -> DataType
$cdataCast1 :: forall (p :: Nat) (t :: * -> *) (c :: * -> *).
(KnownNat p, Typeable t) =>
(forall d. Data d => c (t d)) -> Maybe (c (RealNumber p))
dataCast1 :: forall (t :: * -> *) (c :: * -> *).
Typeable t =>
(forall d. Data d => c (t d)) -> 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))
dataCast2 :: forall (t :: * -> * -> *) (c :: * -> *).
Typeable t =>
(forall d e. (Data d, Data e) => c (t d e))
-> Maybe (c (RealNumber p))
$cgmapT :: forall (p :: Nat).
KnownNat p =>
(forall b. Data b => b -> b) -> RealNumber p -> RealNumber p
gmapT :: (forall b. Data b => b -> b) -> RealNumber p -> RealNumber p
$cgmapQl :: forall (p :: Nat) r r'.
KnownNat p =>
(r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> RealNumber p -> r
gmapQl :: forall r r'.
(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
gmapQr :: forall r r'.
(r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> RealNumber p -> r
$cgmapQ :: forall (p :: Nat) u.
KnownNat p =>
(forall d. Data d => d -> u) -> RealNumber p -> [u]
gmapQ :: forall u. (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
gmapQi :: forall u. Int -> (forall d. Data d => d -> u) -> RealNumber p -> u
$cgmapM :: forall (p :: Nat) (m :: * -> *).
(KnownNat p, Monad m) =>
(forall d. Data d => d -> m d) -> RealNumber p -> m (RealNumber p)
gmapM :: forall (m :: * -> *).
Monad m =>
(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)
gmapMp :: forall (m :: * -> *).
MonadPlus m =>
(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)
gmapMo :: forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> RealNumber p -> m (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 (p :: Nat). Integer -> RealNumber p
forall (p :: Nat). RealNumber p -> RealNumber p
forall (p :: Nat). RealNumber p -> RealNumber p -> RealNumber p
forall a.
(a -> a -> a)
-> (a -> a -> a)
-> (a -> a -> a)
-> (a -> a)
-> (a -> a)
-> (a -> a)
-> (Integer -> a)
-> Num a
$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
* :: RealNumber p -> RealNumber p -> RealNumber p
$cnegate :: forall (p :: Nat). RealNumber p -> RealNumber p
negate :: RealNumber p -> RealNumber p
$cabs :: forall (p :: Nat). RealNumber p -> RealNumber p
abs :: RealNumber p -> RealNumber p
$csignum :: forall (p :: Nat). RealNumber p -> RealNumber p
signum :: RealNumber p -> RealNumber p
$cfromInteger :: forall (p :: Nat). Integer -> RealNumber p
fromInteger :: Integer -> 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 (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
forall a.
Num a =>
(a -> a -> a) -> (a -> a) -> (Rational -> a) -> Fractional a
$c/ :: forall (p :: Nat). RealNumber p -> RealNumber p -> RealNumber p
/ :: RealNumber p -> RealNumber p -> RealNumber p
$crecip :: forall (p :: Nat). RealNumber p -> RealNumber p
recip :: RealNumber p -> RealNumber p
$cfromRational :: forall (p :: Nat). Rational -> RealNumber p
fromRational :: Rational -> 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 (p :: Nat). Num (RealNumber p)
forall (p :: Nat). Ord (RealNumber p)
forall (p :: Nat). RealNumber p -> Rational
forall a. (Num a, Ord a) => (a -> Rational) -> Real a
$ctoRational :: forall (p :: Nat). RealNumber p -> Rational
toRational :: RealNumber p -> Rational
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)
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)
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
$cproperFraction :: forall (p :: Nat) b.
Integral b =>
RealNumber p -> (b, RealNumber p)
properFraction :: forall b. Integral b => RealNumber p -> (b, RealNumber p)
$ctruncate :: forall (p :: Nat) b. Integral b => RealNumber p -> b
truncate :: forall b. Integral b => RealNumber p -> b
$cround :: forall (p :: Nat) b. Integral b => RealNumber p -> b
round :: forall b. Integral b => RealNumber p -> b
$cceiling :: forall (p :: Nat) b. Integral b => RealNumber p -> b
ceiling :: forall b. Integral b => RealNumber p -> b
$cfloor :: forall (p :: Nat) b. Integral b => RealNumber p -> b
floor :: forall b. Integral b => RealNumber p -> b
RealFrac,(forall x. RealNumber p -> Rep (RealNumber p) x)
-> (forall x. Rep (RealNumber p) x -> RealNumber p)
-> Generic (RealNumber p)
forall (p :: Nat) x. Rep (RealNumber p) x -> RealNumber p
forall (p :: Nat) x. RealNumber p -> Rep (RealNumber p) x
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
$cfrom :: forall (p :: Nat) x. RealNumber p -> Rep (RealNumber p) x
from :: forall x. RealNumber p -> Rep (RealNumber p) x
$cto :: forall (p :: Nat) x. Rep (RealNumber p) x -> RealNumber p
to :: forall x. Rep (RealNumber p) x -> RealNumber p
Generic,[RealNumber p] -> Value
[RealNumber p] -> Encoding
RealNumber p -> Bool
RealNumber p -> Value
RealNumber p -> Encoding
(RealNumber p -> Value)
-> (RealNumber p -> Encoding)
-> ([RealNumber p] -> Value)
-> ([RealNumber p] -> Encoding)
-> (RealNumber p -> Bool)
-> ToJSON (RealNumber p)
forall (p :: Nat). [RealNumber p] -> Value
forall (p :: Nat). [RealNumber p] -> Encoding
forall (p :: Nat). RealNumber p -> Bool
forall (p :: Nat). RealNumber p -> Value
forall (p :: Nat). RealNumber p -> Encoding
forall a.
(a -> Value)
-> (a -> Encoding)
-> ([a] -> Value)
-> ([a] -> Encoding)
-> (a -> Bool)
-> ToJSON a
$ctoJSON :: forall (p :: Nat). RealNumber p -> Value
toJSON :: RealNumber p -> Value
$ctoEncoding :: forall (p :: Nat). RealNumber p -> Encoding
toEncoding :: RealNumber p -> Encoding
$ctoJSONList :: forall (p :: Nat). [RealNumber p] -> Value
toJSONList :: [RealNumber p] -> Value
$ctoEncodingList :: forall (p :: Nat). [RealNumber p] -> Encoding
toEncodingList :: [RealNumber p] -> Encoding
$comitField :: forall (p :: Nat). RealNumber p -> Bool
omitField :: RealNumber p -> Bool
ToJSON,Maybe (RealNumber p)
Value -> Parser [RealNumber p]
Value -> Parser (RealNumber p)
(Value -> Parser (RealNumber p))
-> (Value -> Parser [RealNumber p])
-> Maybe (RealNumber p)
-> FromJSON (RealNumber p)
forall (p :: Nat). Maybe (RealNumber p)
forall (p :: Nat). Value -> Parser [RealNumber p]
forall (p :: Nat). Value -> Parser (RealNumber p)
forall a.
(Value -> Parser a)
-> (Value -> Parser [a]) -> Maybe a -> FromJSON a
$cparseJSON :: forall (p :: Nat). Value -> Parser (RealNumber p)
parseJSON :: Value -> Parser (RealNumber p)
$cparseJSONList :: forall (p :: Nat). Value -> Parser [RealNumber p]
parseJSONList :: Value -> Parser [RealNumber p]
$comittedField :: forall (p :: Nat). Maybe (RealNumber p)
omittedField :: Maybe (RealNumber p)
FromJSON,RealNumber p -> ()
(RealNumber p -> ()) -> NFData (RealNumber p)
forall (p :: Nat). RealNumber p -> ()
forall a. (a -> ()) -> NFData a
$crnf :: forall (p :: Nat). RealNumber p -> ()
rnf :: RealNumber p -> ()
NFData)
data NatPrec (p :: Nat) = NatPrec
instance KnownNat p => HasResolution (NatPrec p) where
resolution :: forall (p :: * -> *). 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 (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
. 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 Random (RealNumber p) where
randomR :: forall g.
RandomGen g =>
(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
v <- Rand g (RealNumber p)
forall a. Random a => RandT g Identity a
forall (m :: * -> *) a. (MonadRandom m, Random a) => m a
getRandom
pure $ (b-a)*abs v + a
random :: forall g. RandomGen g => 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
v <- RandT g Identity Int
forall a. Random a => RandT g Identity a
forall (m :: * -> *) a. (MonadRandom m, Random a) => m a
getRandom
let fromInt :: Int -> Integer; fromInt = Int -> Integer
forall a b. (Integral a, Num b) => a -> b
fromIntegral
pure $ RealNumber $ fromInt v % fromInt 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
$cshowsPrec :: forall k (p :: k). HasResolution p => Int -> AsFixed p -> ShowS
showsPrec :: Int -> AsFixed p -> ShowS
$cshow :: forall k (p :: k). HasResolution p => AsFixed p -> String
show :: AsFixed p -> String
$cshowList :: forall k (p :: k). HasResolution p => [AsFixed p] -> ShowS
showList :: [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
$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
/= :: AsFixed p -> AsFixed p -> Bool
Eq)
toFixed :: KnownNat p => RealNumber p -> Fixed (NatPrec p)
toFixed :: forall (p :: Nat). KnownNat p => 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 :: forall (p :: Nat). KnownNat p => 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 :: forall (p :: Nat).
KnownNat p =>
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