-- Hoogle documentation, generated by Haddock -- See Hoogle, http://www.haskell.org/hoogle/ -- | variable-precision floating point -- -- Software floating point with type-tagged variable mantissa precision, -- implemented using a strict pair of Integer and Int -- scaled alike to decodeFloat. Version 0.2.1 added a fixed point -- number type. -- -- Instances of the usual numeric type classes are provided, along with -- additional operators (with carefully chosen fixities) to coerce, -- adjust and reify precisions. -- -- The intention with this library is to be relatively simple but still -- useful, refer to the documentation for caveats concerning accuracy and -- assorted ill-behaviour. -- -- Usage with ghc(i)-7.0.4 might require -fcontext-stack=100. @package variable-precision @version 0.2.1 -- | Boilerplate definitions generated by: -- --
--   flip mapM_ [16..53] $ \p -> let s = show p in
--     putStrLn $ "type N" ++ s ++ " = SuccessorTo N" ++ show (p - 1) ++
--       " ; n" ++ s ++ " :: N" ++ s ++ " ; n" ++ s ++ " = undefined"
--   
module TypeLevel.NaturalNumber.ExtraNumbers type N16 = SuccessorTo N15 n16 :: N16 type N17 = SuccessorTo N16 n17 :: N17 type N18 = SuccessorTo N17 n18 :: N18 type N19 = SuccessorTo N18 n19 :: N19 type N20 = SuccessorTo N19 n20 :: N20 type N21 = SuccessorTo N20 n21 :: N21 type N22 = SuccessorTo N21 n22 :: N22 type N23 = SuccessorTo N22 n23 :: N23 type N24 = SuccessorTo N23 n24 :: N24 type N25 = SuccessorTo N24 n25 :: N25 type N26 = SuccessorTo N25 n26 :: N26 type N27 = SuccessorTo N26 n27 :: N27 type N28 = SuccessorTo N27 n28 :: N28 type N29 = SuccessorTo N28 n29 :: N29 type N30 = SuccessorTo N29 n30 :: N30 type N31 = SuccessorTo N30 n31 :: N31 type N32 = SuccessorTo N31 n32 :: N32 type N33 = SuccessorTo N32 n33 :: N33 type N34 = SuccessorTo N33 n34 :: N34 type N35 = SuccessorTo N34 n35 :: N35 type N36 = SuccessorTo N35 n36 :: N36 type N37 = SuccessorTo N36 n37 :: N37 type N38 = SuccessorTo N37 n38 :: N38 type N39 = SuccessorTo N38 n39 :: N39 type N40 = SuccessorTo N39 n40 :: N40 type N41 = SuccessorTo N40 n41 :: N41 type N42 = SuccessorTo N41 n42 :: N42 type N43 = SuccessorTo N42 n43 :: N43 type N44 = SuccessorTo N43 n44 :: N44 type N45 = SuccessorTo N44 n45 :: N45 type N46 = SuccessorTo N45 n46 :: N46 type N47 = SuccessorTo N46 n47 :: N47 type N48 = SuccessorTo N47 n48 :: N48 type N49 = SuccessorTo N48 n49 :: N49 type N50 = SuccessorTo N49 n50 :: N50 type N51 = SuccessorTo N50 n51 :: N51 type N52 = SuccessorTo N51 n52 :: N52 type N53 = SuccessorTo N52 n53 :: N53 -- | Implementations of various floating point algorithms. Accuracy has not -- been extensively verified, and termination has not been proven. -- -- Everything assumes that floatRadix is 2. This is *not* checked. -- -- Functions taking an accuracy parameter may fail to terminate -- if accuracy is too small. Accuracy is measured in least -- significant bits, similarly to '(=~=)'. -- -- In this documentation, basic functionality denotes that methods -- used are from classes: -- -- -- -- Further, basic RealFloat functionality denotes basic -- functionality with the addition of: -- -- -- -- The intention behind the used functionality documentation is to help -- users decide when it is appropriate to use these generic -- implementations to implement instances. module Numeric.VariablePrecision.Algorithms -- | Convert between generic RealFloat types more efficiently than -- realToFrac. Tries hard to preserve special values like -- infinities and negative zero, but any NaN payload is lost. -- -- Uses only basic RealFloat functionality. recodeFloat :: (RealFloat a, RealFloat b) => a -> b -- | Lift a function from Double to generic RealFloat types. viaDouble :: (RealFloat a, RealFloat b) => (Double -> Double) -> a -> b -- | Approximate equality. (a =~= b) c when adding the difference -- to the larger in magnitude changes at most c least -- significant mantissa bits. -- -- Uses only basic RealFloat functionality. (=~=) :: RealFloat a => a -> a -> Int -> Bool -- | Compute a reciprocal using the Newton-Raphson division algorithm, as -- described in -- http://en.wikipedia.org/wiki/Division_%28digital%29#Newton.E2.80.93Raphson_division. -- -- Uses only basic RealFloat functionality. genericRecip :: RealFloat a => Int -> a -> a -- | Compute a square root using Newton's method. -- -- Uses basic RealFloat functionality and '(/)'. genericSqrt :: RealFloat a => Int -> a -> a -- | Compute an exponential using power series. -- -- Uses basic RealFloat functionality, '(/)' and recip. genericExp :: RealFloat a => Int -> a -> a -- | Compute a logarithm. -- -- See genericLog'' for algorithmic references. -- -- Uses basic RealFloat functionality, sqrt and recip. genericLog :: RealFloat a => Int -> a -> a -- | Compute a logarithm using decomposition and a value for log -- 2. -- -- See genericLog'' for algorithmic references. -- -- Uses basic RealFloat functionality, sqrt, and recip. genericLog' :: RealFloat a => Int -> a -> a -> a -- | Compute log 2. -- -- See genericLog'' for algorithmic references. -- -- Uses basic RealFloat functionality, sqrt and recip. genericLog2 :: RealFloat a => Int -> a -- | Compute a logarithm for a value in [0.5,1) using the AGM method as -- described in section 7 of The Logarithmic Constant: log 2 -- Xavier Gourdon and Pascal Sebah, May 18, 2010, -- http://numbers.computation.free.fr/Constants/Log2/log2.ps. -- -- The precondition is not checked. -- -- Uses basic RealFloat functionality, sqrt, and recip. genericLog'' :: RealFloat a => Int -> a -> a -- | Compute pi using the method described in section 8 of -- Multiple-precision zero-finding methods and the complexity of -- elementary function evaluation Richard P Brent, 1975 (revised May -- 30, 2010), http://arxiv.org/abs/1004.3412. -- -- Uses basic RealFloat functionality, '(/)', and sqrt. genericPi :: RealFloat a => Int -> a -- | Special values implemented using basic RealFloat functionality. genericPositiveZero, genericNotANumber, genericNegativeInfinity, genericPositiveInfinity, genericNegativeZero :: RealFloat a => a -- | Check if two numbers have the same sign. May give a nonsense result if -- an argument is NaN. sameSign :: (Ord a, Num a) => a -> a -> Bool -- | Classes for types with precision represented by a type-level natural -- number, and variable precision types. -- -- Note that performance may be (even) slow(er) with some versions of the -- type-level-natural-number package. module Numeric.VariablePrecision.Precision -- | A class for types with precision. The methods must not evaluate their -- arguments, and their results must not be evaluated. Minimal complete -- definition: (none). class HasPrecision t where precisionOf _ = undefined precisionOf :: (HasPrecision t, NaturalNumber p) => t p -> p -- | Much like naturalNumberAsInt combined with precisionOf. precision :: (NaturalNumber p, HasPrecision t) => t p -> Word -- | Much like const with a restricted type. atPrecision :: (NaturalNumber p, HasPrecision t) => t p -> p -> t p -- | Much like const with a restricted type. Precedence between -- < and +. atPrecisionOf :: (HasPrecision t, HasPrecision s) => t p -> s p -> t p -- | An alias for atPrecisionOf. Precedence between < and -- +. (.@) :: (HasPrecision t, HasPrecision s) => t p -> s p -> t p -- | A class for types with adjustable precision. Minimal complete -- definition: adjustPrecision. class HasPrecision t => VariablePrecision t adjustPrecision :: (VariablePrecision t, NaturalNumber p, NaturalNumber q) => t p -> t q -- | Synonym for adjustPrecision. auto :: (VariablePrecision t, NaturalNumber p, NaturalNumber q) => t p -> t q -- | Much like adjustPrecision combined with atPrecision. withPrecision :: (NaturalNumber p, NaturalNumber q, VariablePrecision t) => t p -> q -> t q -- | Much like withPrecision combined with precisionOf. -- Precedence between < and +. withPrecisionOf :: (NaturalNumber p, NaturalNumber q, VariablePrecision t, HasPrecision s) => t p -> s q -> t q -- | An alias for withPrecisionOf. Precedence between < -- and +. (.@~) :: (NaturalNumber p, NaturalNumber q, VariablePrecision t, HasPrecision s) => t p -> s q -> t q -- | Reify from value-level to type-level using Rank2Types. module Numeric.VariablePrecision.Precision.Reify -- | Reify a precision from value-level to type-level. reifyPrecision :: Word -> (forall p. NaturalNumber p => p -> a) -> a -- | Much like reifyPrecision combined with withPrecision. withReifiedPrecision :: (VariablePrecision t, NaturalNumber p) => t p -> Word -> (forall q. NaturalNumber q => t q -> a) -> a -- | An alias for withReifiedPrecision. (.@$) :: (VariablePrecision t, NaturalNumber p) => t p -> Word -> (forall q. NaturalNumber q => t q -> a) -> a -- | Variable precision software fixed point based on Integer. -- -- Accuracy has not been extensively verified. -- -- Example: -- --
--   reifyPrecision 1000 $ \prec ->
--      show $ auto (355 :: VFixed N15) / 113 `atPrecision` prec
--   
module Numeric.VariablePrecision.Fixed -- | A software implementation of fixed point arithmetic, using an -- Integer adjusted to p bits after the binary point. data VFixed p -- | A concrete format suitable for storage or wire transmission. data DFixed DFixed :: !Word -> !Integer -> DFixed dxPrecision :: DFixed -> !Word dxMantissa :: DFixed -> !Integer -- | Freeze a VFixed. toDFixed :: NaturalNumber p => VFixed p -> DFixed -- | Thaw a DFixed. Results in Nothing on precision mismatch. fromDFixed :: NaturalNumber p => DFixed -> Maybe (VFixed p) -- | Thaw a DFixed to its natural precision. withDFixed :: DFixed -> (forall p. NaturalNumber p => VFixed p -> r) -> r instance Typeable1 VFixed instance Typeable DFixed instance Data p => Data (VFixed p) instance Eq DFixed instance Ord DFixed instance Read DFixed instance Show DFixed instance Data DFixed instance NaturalNumber p => RealFrac (VFixed p) instance NaturalNumber p => Real (VFixed p) instance NaturalNumber p => Fractional (VFixed p) instance NaturalNumber p => Num (VFixed p) instance NaturalNumber p => Ord (VFixed p) instance NaturalNumber p => Eq (VFixed p) instance NaturalNumber p => Read (VFixed p) instance NaturalNumber p => Show (VFixed p) instance NaturalNumber p => BinDecode (VFixed p) instance VariablePrecision VFixed instance HasPrecision VFixed -- | Variable precision software floating point based on (Integer, -- Int) as used by decodeFloat. Supports infinities and NaN, -- but not negative zero or denormalization. -- -- Accuracy has not been extensively verified, and termination of -- numerical algorithms has not been proven. module Numeric.VariablePrecision.Float -- | A software implementation of floating point arithmetic, using a strict -- pair of Integer and Int, scaled similarly to -- decodeFloat, along with additional values representing: -- -- -- -- The Floating instance so far only implements algorithms for: -- -- -- -- These Floating methods transit via Double and so have -- limited precision: -- -- -- -- floatRange is arbitrarily limited to mitigate the problems that -- occur when enormous integers might be needed during some number type -- conversions (worst case consequence: program abort in gmp). data VFloat p -- | A selection of norms. class HasPrecision t => Normed t norm1 :: (Normed t, NaturalNumber p) => t p -> VFloat p norm2 :: (Normed t, NaturalNumber p) => t p -> VFloat p norm2Squared :: (Normed t, NaturalNumber p) => t p -> VFloat p normInfinity :: (Normed t, NaturalNumber p) => t p -> VFloat p -- | A measure of meaningful precision in the difference of two finite -- non-zero values. -- -- Values of very different magnitude have little meaningful difference, -- because a + b approxEq a when |a| >> -- |b|. -- -- Very close values have little meaningful difference, because a + -- (a - b) approxEq a as |a| >> |a - b|. -- -- effectivePrecisionWith attempts to quantify this. effectivePrecisionWith :: (Num t, RealFloat r) => (t -> r) -> t -> t -> Int -- | Much like effectivePrecisionWith combined with -- normInfinity. effectivePrecision :: (NaturalNumber p, HasPrecision t, Normed t, Num (t p)) => t p -> t p -> Int -- | An alias for effectivePrecision. (-@?) :: (NaturalNumber p, HasPrecision t, Normed t, Num (t p)) => t p -> t p -> Int -- | A concrete format suitable for storage or wire transmission. data DFloat DFloat :: !Word -> !Integer -> !Int -> DFloat dPrecision :: DFloat -> !Word dMantissa :: DFloat -> !Integer dExponent :: DFloat -> !Int DZero :: !Word -> DFloat dPrecision :: DFloat -> !Word DPositiveInfinity :: !Word -> DFloat dPrecision :: DFloat -> !Word DNegativeInfinity :: !Word -> DFloat dPrecision :: DFloat -> !Word DNotANumber :: !Word -> DFloat dPrecision :: DFloat -> !Word -- | Freeze a VFloat. toDFloat :: NaturalNumber p => VFloat p -> DFloat -- | Thaw a DFloat. Results in Nothing on precision mismatch. fromDFloat :: NaturalNumber p => DFloat -> Maybe (VFloat p) -- | Thaw a DFloat to its natural precision. withDFloat :: DFloat -> (forall p. NaturalNumber p => VFloat p -> r) -> r instance Typeable1 VFloat instance Typeable DFloat instance Data p => Data (VFloat p) instance Eq DFloat instance Ord DFloat instance Read DFloat instance Show DFloat instance Data DFloat instance Normed VFloat instance NaturalNumber p => Floating (VFloat p) instance NaturalNumber p => RealFloat (VFloat p) instance NaturalNumber p => RealFrac (VFloat p) instance NaturalNumber p => Fractional (VFloat p) instance NaturalNumber p => Real (VFloat p) instance NaturalNumber p => Num (VFloat p) instance Ord (VFloat p) instance Eq (VFloat p) instance VariablePrecision VFloat instance HasPrecision VFloat instance NaturalNumber p => Read (VFloat p) instance NaturalNumber p => Show (VFloat p) instance NaturalNumber p => FShow (VFloat p) instance NaturalNumber p => DispFloat (VFloat p) -- | Newtype wrapper around Complex. When both of Complex and -- this module need to be imported, use qualified imports. module Numeric.VariablePrecision.Complex -- | Newtype wrapper around Complex so that instances can be written -- for HasPrecision and VariablePrecision. data VComplex p -- | Alike to :+, constructs a complex number from a real part and -- an imaginary part. (.+) :: NaturalNumber p => VFloat p -> VFloat p -> VComplex p -- | Real-complex multiplication. (.*) :: NaturalNumber p => VFloat p -> VComplex p -> VComplex p -- | Complex-real multiplication. (*.) :: NaturalNumber p => VComplex p -> VFloat p -> VComplex p -- | Convert VComplex to Complex. toComplex :: VComplex p -> Complex (VFloat p) -- | Convert Complex to VComplex. fromComplex :: Complex (VFloat p) -> VComplex p -- | Lift an operation on Complex to one on VComplex. withComplex :: (Complex (VFloat p) -> Complex (VFloat q)) -> (VComplex p -> VComplex q) -- | Apply a function to both components of a complex number. mapComplex :: (RealFloat a, RealFloat b) => (a -> b) -> Complex a -> Complex b -- | Much like mapComplex recodeFloat. recodeComplex :: (RealFloat a, RealFloat b) => Complex a -> Complex b -- | Much like withComplex scaleComplex'. scaleComplex :: NaturalNumber p => Int -> VComplex p -> VComplex p -- | Real part. realPart :: NaturalNumber p => VComplex p -> VFloat p -- | Imaginary part. imagPart :: NaturalNumber p => VComplex p -> VFloat p -- | Conjugate. conjugate :: NaturalNumber p => VComplex p -> VComplex p -- | Magnitude. magnitude :: NaturalNumber p => VComplex p -> VFloat p -- | Magnitude squared. magnitude2 :: NaturalNumber p => VComplex p -> VFloat p -- | Complex square. sqr :: NaturalNumber p => VComplex p -> VComplex p -- | Phase. phase :: NaturalNumber p => VComplex p -> VFloat p -- | Polar form. polar :: NaturalNumber p => VComplex p -> (VFloat p, VFloat p) -- | Unit at phase. cis :: NaturalNumber p => VFloat p -> VComplex p -- | From polar form. mkPolar :: NaturalNumber p => VFloat p -> VFloat p -> VComplex p -- | Much like mapComplex scaleFloat. scaleComplex' :: RealFloat r => Int -> Complex r -> Complex r -- | Magnitude squared. magnitude2' :: RealFloat r => Complex r -> r -- | Complex square. sqr' :: RealFloat r => Complex r -> Complex r -- | A concrete format suitable for storage or wire transmission. data DComplex DComplex :: !DFloat -> !DFloat -> DComplex dRealPart :: DComplex -> !DFloat dImagPart :: DComplex -> !DFloat -- | Freeze a VComplex. toDComplex :: NaturalNumber p => VComplex p -> DComplex -- | Thaw a DComplex. Results in Nothing on precision -- mismatch. fromDComplex :: NaturalNumber p => DComplex -> Maybe (VComplex p) -- | Thaw a DComplex to its natural precision. Nothing is -- passed on precision mismatch between real and imaginary parts. withDComplex :: DComplex -> (forall p. NaturalNumber p => Maybe (VComplex p) -> r) -> r instance Typeable1 VComplex instance Typeable DComplex instance Eq (VComplex p) instance NaturalNumber p => Num (VComplex p) instance NaturalNumber p => Fractional (VComplex p) instance NaturalNumber p => Floating (VComplex p) instance Data p => Data (VComplex p) instance Eq DComplex instance Ord DComplex instance Read DComplex instance Show DComplex instance Data DComplex instance NaturalNumber p => Read (VComplex p) instance NaturalNumber p => Show (VComplex p) instance Normed VComplex instance VariablePrecision VComplex instance HasPrecision VComplex -- | Aliases for recodeFloat and recodeComplex with -- specialized types. -- -- Aliases for commonly desired types. module Numeric.VariablePrecision.Aliases -- | Convert to a Float from the same precision. toFloat :: F24 -> Float -- | Convert from a Float to the same precision. fromFloat :: Float -> F24 -- | Convert to a Double from the same precision. toDouble :: F53 -> Double -- | Convert from a Double to the same precision. fromDouble :: Double -> F53 -- | Convert to a Float from the same precision. toComplexFloat :: C24 -> Complex Float -- | Convert from a Float to the same precision. fromComplexFloat :: Complex Float -> C24 -- | Convert to a Double from the same precision. toComplexDouble :: C53 -> Complex Double -- | Convert from a Double to the same precision. fromComplexDouble :: Complex Double -> C53 type F8 = VFloat N8 type F16 = VFloat N16 type F24 = VFloat N24 type F32 = VFloat N32 type F40 = VFloat N40 type F48 = VFloat N48 type F53 = VFloat N53 f8 :: F8 f16 :: F16 f24 :: F24 f32 :: F32 f40 :: F40 f48 :: F48 f53 :: F53 type C8 = VComplex N8 type C16 = VComplex N16 type C24 = VComplex N24 type C32 = VComplex N32 type C40 = VComplex N40 type C48 = VComplex N48 type C53 = VComplex N53 c8 :: C8 c16 :: C16 c24 :: C24 c32 :: C32 c40 :: C40 c48 :: C48 c53 :: C53 -- | Convenience module. module Numeric.VariablePrecision