numbers 2007.9.24 → 2007.9.25
raw patch · 7 files changed
+53/−8 lines, 7 filesPVP ok
version bump matches the API change (PVP)
API changes (from Hackage documentation)
+ Data.Number.BigFloat: class Epsilon e
+ Data.Number.BigFloat: data Eps1
+ Data.Number.BigFloat: data EpsDiv10 p
+ Data.Number.BigFloat: data Prec10
+ Data.Number.BigFloat: data Prec50
+ Data.Number.BigFloat: data PrecPlus20 e
+ Data.Number.Natural: infinity :: Natural
Files
- Data/Number/BigFloat.hs +10/−1
- Data/Number/CReal.hs +0/−1
- Data/Number/Fixed.hs +17/−3
- Data/Number/Interval.hs +1/−0
- Data/Number/Natural.hs +12/−2
- Data/Number/Symbolic.hs +12/−0
- numbers.cabal +1/−1
Data/Number/BigFloat.hs view
@@ -1,4 +1,12 @@-module Data.Number.BigFloat(BigFloat) where+-- | A simple implementation of floating point numbers with a selectable+-- precision. The number of digits in the mantissa is selected by the+-- 'Epsilon' type class from the "Fixed" module.+--+-- The numbers are stored in base 10.+module Data.Number.BigFloat(+ BigFloat,+ Epsilon, Eps1, EpsDiv10, Prec10, Prec50, PrecPlus20,+ ) where import Numeric(showSigned) import Data.Number.Fixed@@ -9,6 +17,7 @@ -- This representation is stupid, two Integers makes more sense, -- but is more work.+-- | Floating point number where the precision is determined by the type /e/. data BigFloat e = BF (Fixed e) Integer deriving (Eq, Ord)
Data/Number/CReal.hs view
@@ -7,7 +7,6 @@ -- David Lester, Department of Computer Science, Manchester University, M13 9PL. -- (2000-2001) --- #hide module Data.Number.CReal(CReal, showCReal) where import Data.Ratio import Numeric(readFloat, readSigned)
Data/Number/Fixed.hs view
@@ -1,36 +1,47 @@ {-# OPTIONS_GHC -fglasgow-exts -fscoped-type-variables #-}-module Data.Number.Fixed(Fixed, Epsilon, Eps1, EpsDiv10, Prec10, Prec50, PrecPlus20,- convertFixed, dynamicEps, precision) where+-- | Numbers with a fixed number of decimals.+module Data.Number.Fixed(+ Fixed,+ Epsilon, Eps1, EpsDiv10, Prec10, Prec50, PrecPlus20,+ convertFixed, dynamicEps, precision) where import Numeric import Data.Char import Data.Ratio import qualified Data.Number.FixedFunctions as F +-- | The 'Epsilon' class contains the types that can be used to determine the+-- precision of a 'Fixed' number. class Epsilon e where eps :: e -> Rational +-- | An epsilon of 1, i.e., no decimals. data Eps1 instance Epsilon Eps1 where eps _ = 1 +-- | A type construct that gives one more decimals than the argument. data EpsDiv10 p instance (Epsilon e) => Epsilon (EpsDiv10 e) where eps e = eps (un e) / 10 where un :: EpsDiv10 e -> e un = undefined +-- | Ten decimals. data Prec10 instance Epsilon Prec10 where eps _ = 1e-10 +-- | 50 decimals. data Prec50 instance Epsilon Prec50 where eps _ = 1e-50 +-- | 500 decimals. data Prec500 instance Epsilon Prec500 where eps _ = 1e-500 +-- A type that gives 20 more decimals than the argument. data PrecPlus20 e instance (Epsilon e) => Epsilon (PrecPlus20 e) where eps e = 1e-20 * eps (un e)@@ -39,8 +50,10 @@ ----------- +-- The type of fixed precision numbers. The type /e/ determines the precision. newtype Fixed e = F Rational deriving (Eq, Ord, Enum, Real, RealFrac) +-- Get the accuracy (the epsilon) of the type. precision :: (Epsilon e) => Fixed e -> Rational precision = getEps @@ -64,6 +77,7 @@ approx :: Rational -> Rational -> Rational approx x eps = approxRational x (eps/2) +-- | Convert between two arbitrary fixed precision types. convertFixed :: (Epsilon e, Epsilon f) => Fixed e -> Fixed f convertFixed e@(F x) = f where f = F $ if feps > eeps then approx x feps else x@@ -125,7 +139,7 @@ ----------- -+-- The call @dynmicEps r f v@ evaluates @f v@ to a precsion of @r@. dynamicEps :: forall a . Rational -> (forall e . Epsilon e => Fixed e -> a) -> Rational -> a dynamicEps r f v = loop (undefined :: Eps1) where loop :: forall x . (Epsilon x) => x -> a
Data/Number/Interval.hs view
@@ -1,3 +1,4 @@+-- | An incomplete implementation of interval aritrhmetic. module Data.Number.Interval(Interval, ival, getIval) where data Interval a = I a a
Data/Number/Natural.hs view
@@ -1,4 +1,10 @@-module Data.Number.Natural(Natural) where+-- | Lazy natural numbers.+-- Addition and multiplication recurses over the first argument, i.e.,+-- @1 + n@ is the way to write the constant time successor function.+--+-- Not that (+) and (*) are not commutative for lazy natural numbers+-- when considering bottom.+module Data.Number.Natural(Natural, infinity) where data Natural = Z | S Natural @@ -40,7 +46,7 @@ (0, x) else let (q, r) = quotRem (x-y) y- in (q+1, r)+ in (1+q, r) div = quot mod = rem toInteger Z = 0@@ -52,3 +58,7 @@ instance Enum Natural where toEnum = fromIntegral fromEnum = fromIntegral++-- | The infinite natural number.+infinity :: Natural+infinity = S infinity
Data/Number/Symbolic.hs view
@@ -1,9 +1,15 @@+-- | Symbolic number, i.e., these are not numbers at all, but just build+-- a representation of the expressions.+-- This implementation is incomplete in that it allows comnstruction,+-- but not deconstruction of the expressions. It's mainly useful for+-- debugging. module Data.Number.Symbolic(Sym, var, con, subst, unSym) where import Data.Char(isAlpha) import Data.Maybe(fromMaybe) import Debug.Trace +-- | Symbolic numbers over some base type for the literals. data Sym a = Con a | App String ([a]->a) [Sym a] instance (Eq a) => Eq (Sym a) where@@ -17,12 +23,16 @@ App _ _ _ `compare` Con _ = GT App f _ xs `compare` App f' _ xs' = (f, xs) `compare` (f', xs') +-- | Create a variable. var :: String -> Sym a var s = App s undefined [] +-- | Create a constant (useful when it is not a literal). con :: a -> Sym a con = Con +-- | The expression @subst x v e@ substitutes the expression @v@ for each+-- occurence of the variable @x@ in @e@. subst :: (Num a) => String -> Sym a -> Sym a -> Sym a subst _ _ e@(Con _) = e subst x v e@(App x' _ []) | x == x' = v@@ -33,6 +43,8 @@ [e1,e2] -> binOp (\ x y -> f [x,y]) e1 s e2 es' -> App s f es' +-- Turn a symbolic number into a regular one if it is a constant,+-- otherwise generate an error. unSym :: (Show a) => Sym a -> a unSym (Con c) = c unSym e = error $ "unSym called: " ++ show e
numbers.cabal view
@@ -1,5 +1,5 @@ Name: numbers-Version: 2007.9.24+Version: 2007.9.25 License: BSD3 Author: Lennart Augustsson Maintainer: Lennart Augustsson