packages feed

MicroHs-0.17.4.0 / lib / Data / Fixed.hs

------------------------------------------------------------------------------- |-- Module      :  Data.Fixed-- Copyright   :  (c) Ashley Yakeley 2005, 2006, 2009-- License     :  BSD-style (see the file libraries/base/LICENSE)---- Maintainer  :  Ashley Yakeley <ashley@semantic.org>-- Stability   :  stable-- Portability :  portable-----------------------------------------------------------------------------module Data.Fixed(   -- * The Fixed Type    Fixed(..), HasResolution(..),    showFixed,    -- ** 1\/1    E0,Uni,    -- ** 1\/10    E1,Deci,    -- ** 1\/100    E2,Centi,    -- ** 1\/1 000    E3,Milli,    -- ** 1\/1 000 000    E6,Micro,    -- ** 1\/1 000 000 000    E9,Nano,    -- ** 1\/1 000 000 000 000    E12,Pico,    -- * Generalized Functions on Real's    div',    mod',    divMod') whereimport qualified Prelude()import MiniPreludeimport Data.TypeLits (KnownNat, natVal)import Text.Read.Internalimport Text.ParserCombinators.ReadPrecimport Text.Read.Leximport Data.Coerceimport Data.Dataimport Data.Doubleimport Data.Floatingimport Data.Fractionalimport Data.Integerimport Data.Realimport Data.RealFracimport Data.Typeabledefault () -- avoid any defaulting shenanigansdiv' :: (Real a,Integral b) => a -> a -> bdiv' n d = floor (toRational n / toRational d)divMod' :: (Real a,Integral b) => a -> a -> (b,a)divMod' n d = (f,n - fromIntegral f * d) where    f = div' n dmod' :: (Real a) => a -> a -> amod' n d = n - fromInteger f * d where    f = div' n dtype Fixed :: forall k . k -> Typenewtype Fixed a = MkFixed Integer        deriving ( Eq  -- ^ @since 2.01                 , Ord -- ^ @since 2.01                 )tyFixed :: DataTypetyFixed = mkDataType "Data.Fixed.Fixed" [conMkFixed]conMkFixed :: ConstrconMkFixed = mkConstr tyFixed "MkFixed" [] Prefix-- | @since 4.1.0.0instance (Typeable a) => Data (Fixed a) where    gfoldl k z (MkFixed a) = k (z MkFixed) a    gunfold k z _ = k (z MkFixed)    dataTypeOf _ = tyFixed    toConstr _ = conMkFixedtype HasResolution :: forall k . k -> Constraintclass HasResolution a where    resolution :: p a -> Integerinstance KnownNat n => HasResolution n where    resolution _ = natVal (Proxy :: Proxy n)withType :: (Proxy a -> f a) -> f awithType foo = foo ProxywithResolution :: (HasResolution a) => (Integer -> f a) -> f awithResolution foo = withType (foo . resolution)instance Enum (Fixed a) where    succ (MkFixed a) = MkFixed (succ a)    pred (MkFixed a) = MkFixed (pred a)    toEnum = MkFixed . toEnum    fromEnum (MkFixed a) = fromEnum a    enumFrom (MkFixed a) = coerce (enumFrom a)    enumFromThen (MkFixed a) (MkFixed b) = coerce (enumFromThen a b)    enumFromTo (MkFixed a) (MkFixed b) = coerce (enumFromTo a b)    enumFromThenTo (MkFixed a) (MkFixed b) (MkFixed c) = coerce (enumFromThenTo a b c)instance (HasResolution a) => Num (Fixed a) where    (MkFixed a) + (MkFixed b) = MkFixed (a + b)    (MkFixed a) - (MkFixed b) = MkFixed (a - b)    fa@(MkFixed a) * (MkFixed b) = MkFixed (div (a * b) (resolution fa))    negate (MkFixed a) = MkFixed (negate a)    abs (MkFixed a) = MkFixed (abs a)    signum (MkFixed a) = fromInteger (signum a)    fromInteger i = withResolution (\res -> MkFixed (i * res))instance (HasResolution a) => Real (Fixed a) where    toRational fa@(MkFixed a) = toRational a / toRational (resolution fa)instance (HasResolution a) => Fractional (Fixed a) where    fa@(MkFixed a) / (MkFixed b) = MkFixed (div (a * resolution fa) b)    recip fa@(MkFixed a) = MkFixed (div (res * res) a) where        res = resolution fa    fromRational r = withResolution (\res -> MkFixed (floor (r * toRational res)))instance (HasResolution a) => RealFrac (Fixed a) where    properFraction a = (i,a - fromIntegral i) where        i = truncate a    truncate f = truncate (toRational f)    round f = round (toRational f)    ceiling f = ceiling (toRational f)    floor f = floor (toRational f)chopZeros :: Integer -> StringchopZeros 0 = ""chopZeros a | mod a 10 == 0 = chopZeros (div a 10)chopZeros a = show ashowIntegerZeros :: Bool -> Int -> Integer -> StringshowIntegerZeros True _ 0 = ""showIntegerZeros chopTrailingZeros digits a = replicate (digits - length s) '0' ++ s' where    s = show a    s' = if chopTrailingZeros then chopZeros a else swithDot :: String -> StringwithDot "" = ""withDot s = '.':sshowFixed :: (HasResolution a) => Bool -> Fixed a -> StringshowFixed chopTrailingZeros fa@(MkFixed a) | a < 0 = "-" ++ showFixed chopTrailingZeros (asTypeOf (MkFixed (negate a)) fa)showFixed chopTrailingZeros fa@(MkFixed a) = show i ++ withDot (showIntegerZeros chopTrailingZeros digits fracNum) where    res = resolution fa    (i,d) = divMod a res    -- enough digits to be unambiguous    digits = ceiling (logBase 10 (fromInteger res) :: Double)    maxnum = 10 ^ digits    -- read floors, so show must ceil for `read . show = id` to hold. See #9240    fracNum = divCeil (d * maxnum) res    divCeil x y = (x + y - 1) `div` yinstance (HasResolution a) => Show (Fixed a) where    showsPrec p n = showParen (p > 6 && n < 0) $ showString $ showFixed False ninstance (HasResolution a) => Read (Fixed a) where    readPrec     = readNumber convertFixed    readListPrec = readListPrecDefault    readList     = readListDefaultconvertFixed :: forall a . HasResolution a => Lexeme -> ReadPrec (Fixed a)convertFixed (Number n) | Just (i, f) <- numberToFixed e n =    return (fromInteger i + (fromInteger f / (10 ^ e)))    where r = resolution (Proxy :: Proxy a)          -- round 'e' up to help make the 'read . show == id' property          -- possible also for cases where 'resolution' is not a          -- power-of-10, such as e.g. when 'resolution = 128'          e = ceiling (logBase 10 (fromInteger r) :: Double)convertFixed _ = pfaildata E0instance HasResolution E0 where    resolution _ = 1type Uni = Fixed E0data E1instance HasResolution E1 where    resolution _ = 10type Deci = Fixed E1data E2instance HasResolution E2 where    resolution _ = 100type Centi = Fixed E2data E3instance HasResolution E3 where    resolution _ = 1000type Milli = Fixed E3data E6instance HasResolution E6 where    resolution _ = 1000000type Micro = Fixed E6data E9instance HasResolution E9 where    resolution _ = 1000000000type Nano = Fixed E9data E12instance HasResolution E12 where    resolution _ = 1000000000000type Pico = Fixed E12