-- Hoogle documentation, generated by Haddock -- See Hoogle, http://www.haskell.org/hoogle/ -- | An implementation of Mike Cowlishaw's -- General Decimal Arithmetic Specification -- -- This package provides an implementation of the General Decimal -- Arithmetic Specification by Mike Cowlishaw. -- -- For details, see http://speleotrove.com/decimal/ @package decimal-arithmetic @version 0.2.0.0 -- | The functions in this module implement conversions between -- Decimal and String as described in the General -- Decimal Arithmetic Specification. -- -- Because these functions are also used to implement Show and -- Read class methods, it is not usually necessary to import this -- module except to use the toEngineeringString function. module Numeric.Decimal.Conversion -- | Convert a number to a string, using scientific notation if an exponent -- is needed. toScientificString :: Decimal p r -> ShowS -- | Convert a number to a string, using engineering notation if an -- exponent is needed. toEngineeringString :: Decimal p r -> ShowS -- | Convert a string to a number, as defined by its abstract -- representation. The string is expected to conform to the numeric -- string syntax described here. toNumber :: ReadP (Decimal PInfinite r) -- | It is not usually necessary to import this module unless you want to -- use the arithmetic operations from Numeric.Decimal.Operation or -- you need precise control over the handling of exceptional conditions -- in an arithmetic computation. module Numeric.Decimal.Arithmetic -- | A context for decimal arithmetic, carrying signal flags, trap enabler -- state, and a trap handler, parameterized by precision p and -- rounding algorithm r data Context p r -- | Return a new context with all signal flags cleared and all traps -- disabled. newContext :: Context p r -- | The current signal flags of the context flags :: Context p r -> Signals -- | Return a new context with all signal flags cleared, all traps enabled -- except for Inexact, Rounded, and Subnormal, using -- a precision of 9 significant decimal digits, and rounding half up. -- Trapped signals simply call throwError with the corresponding -- Exception, and can be caught using catchError. basicDefaultContext :: Context P9 RoundHalfUp -- | Return a new context with all signal flags cleared, all traps disabled -- (IEEE 854 §7), using selectable precision (the IEEE 754 smallest and -- basic formats correspond to precisions P7, P16, or -- P34), and rounding half even (IEEE 754 §4.3.3). extendedDefaultContext :: Context p RoundHalfEven -- | A decimal arithmetic monad parameterized by the precision p -- and rounding algorithm r data Arith p r a -- | Evaluate an arithmetic computation in the given context and return the -- final value (or exception) and resulting context. runArith :: Arith p r a -> Context p r -> (Either (Exception p r) a, Context p r) -- | Evaluate an arithmetic computation in the given context and return the -- final value or exception, discarding the resulting context. evalArith :: Arith p r a -> Context p r -> Either (Exception p r) a -- | A representation of an exceptional condition data Exception p r -- | The signal raised by the exceptional condition exceptionSignal :: Exception p r -> Signal -- | The defined result for the exceptional condition exceptionResult :: Exception p r -> Decimal p r data Signal -- | Raised when the exponent of a result has been altered or constrained -- in order to fit the constraints of a specific concrete representation Clamped :: Signal -- | Raised when a non-zero dividend is divided by zero DivisionByZero :: Signal -- | Raised when a result is not exact (one or more non-zero coefficient -- digits were discarded during rounding) Inexact :: Signal -- | Raised when a result would be undefined or impossible InvalidOperation :: Signal -- | Raised when the exponent of a result is too large to be represented Overflow :: Signal -- | Raised when a result has been rounded (that is, some zero or non-zero -- coefficient digits were discarded) Rounded :: Signal -- | Raised when a result is subnormal (its adjusted exponent is less than -- Emin), before any rounding Subnormal :: Signal -- | Raised when a result is both subnormal and inexact Underflow :: Signal -- | A group of signals can be manipulated as a set. data Signals -- | Create a set of signals from a singleton. signal :: Signal -> Signals -- | Create a set of signals from a list. signals :: [Signal] -> Signals -- | Determine whether a signal is a member of a set. signalMember :: Signal -> Signals -> Bool -- | Set the given signal flag in the context of the current arithmetic -- computation, and call the trap handler if the trap for this signal is -- currently enabled. raiseSignal :: Signal -> Decimal p r -> Arith p r (Decimal p r) -- | Clear the given signal flags from the context of the current -- arithmetic computation. clearFlags :: Signals -> Arith p r () -- | A trap handler function may return a substitute result for the -- operation that caused the exceptional condition, or it may call -- throwError to abort the arithmetic computation (or pass control -- to an enclosing catchError handler). type TrapHandler p r = Exception p r -> Arith p r (Decimal p r) -- | Evaluate an arithmetic computation within a modified context that -- enables the given signals to be trapped by the given handler. The -- previous trap handler (and enabler state) will be restored during any -- trap, as well as upon completion. Any existing trap handlers for -- signals not mentioned remain in effect. trap :: Signals -> TrapHandler p r -> Arith p r a -> Arith p r a instance GHC.Show.Show (Numeric.Decimal.Arithmetic.Exception p r) instance GHC.Classes.Eq Numeric.Decimal.Arithmetic.Signals instance GHC.Show.Show Numeric.Decimal.Arithmetic.Signal instance GHC.Enum.Bounded Numeric.Decimal.Arithmetic.Signal instance GHC.Enum.Enum Numeric.Decimal.Arithmetic.Signal instance GHC.Classes.Eq Numeric.Decimal.Arithmetic.Signal instance GHC.Base.Functor (Numeric.Decimal.Arithmetic.Arith p r) instance GHC.Base.Applicative (Numeric.Decimal.Arithmetic.Arith p r) instance GHC.Base.Monad (Numeric.Decimal.Arithmetic.Arith p r) instance Control.Monad.Error.Class.MonadError (Numeric.Decimal.Arithmetic.Exception p r) (Numeric.Decimal.Arithmetic.Arith p r) instance Control.Monad.State.Class.MonadState (Numeric.Decimal.Arithmetic.Context p r) (Numeric.Decimal.Arithmetic.Arith p r) instance GHC.Show.Show Numeric.Decimal.Arithmetic.Signals instance GHC.Base.Monoid Numeric.Decimal.Arithmetic.Signals -- | Eventually most or all of the arithmetic operations described in the -- General Decimal Arithmetic Specification will be provided here. -- For now, the operations are mostly limited to those exposed through -- various class methods. -- -- It is suggested to import this module qualified to avoid -- Prelude name clashes: -- --
--   import qualified Numeric.Decimal.Operation as Op
--   
-- -- Note that it is not usually necessary to import this module unless you -- want to use operations unavailable through class methods, or you need -- precise control over the handling of exceptional conditions. module Numeric.Decimal.Operation -- | abs takes one operand. If the operand is negative, the result -- is the same as using the minus operation on the operand. -- Otherwise, the result is the same as using the plus operation -- on the operand. -- -- Note that the result of this operation is affected by context and may -- set flags. The copyAbs operation may be used if this is -- not desired. abs :: (Precision p, Rounding r) => Decimal a b -> Arith p r (Decimal p r) -- | add takes two operands. If either operand is a special -- value then the general rules apply. -- -- Otherwise, the operands are added. -- -- The result is then rounded to precision digits if necessary, -- counting from the most significant digit of the result. add :: (Precision p, Rounding r) => Decimal a b -> Decimal c d -> Arith p r (Decimal p r) -- | subtract takes two operands. If either operand is a special -- value then the general rules apply. -- -- Otherwise, the operands are added after inverting the sign used -- for the second operand. -- -- The result is then rounded to precision digits if necessary, -- counting from the most significant digit of the result. subtract :: (Precision p, Rounding r) => Decimal a b -> Decimal c d -> Arith p r (Decimal p r) -- | compare takes two operands and compares their values -- numerically. If either operand is a special value then the -- general rules apply. No flags are set unless an operand is a signaling -- NaN. -- -- Otherwise, the operands are compared, returning -1 if the -- first is less than the second, 0 if they are equal, or -- 1 if the first is greater than the second. compare :: (Precision p, Rounding r) => Decimal a b -> Decimal c d -> Arith p r (Decimal p r) -- | divide takes two operands. If either operand is a special -- value then the general rules apply. -- -- Otherwise, if the divisor is zero then either the Division undefined -- condition is raised (if the dividend is zero) and the result is NaN, -- or the Division by zero condition is raised and the result is an -- Infinity with a sign which is the exclusive or of the signs of the -- operands. -- -- Otherwise, a “long division” is effected. -- -- The result is then rounded to precision digits, if necessary, -- according to the rounding algorithm and taking into account the -- remainder from the division. divide :: (FinitePrecision p, Rounding r) => Decimal a b -> Decimal c d -> Arith p r (Decimal p r) -- | max takes two operands, compares their values numerically, and -- returns the maximum. If either operand is a NaN then the general rules -- apply, unless one is a quiet NaN and the other is numeric, in which -- case the numeric operand is returned. max :: (Precision p, Rounding r) => Decimal a b -> Decimal a b -> Arith p r (Decimal a b) -- | maxMagnitude takes two operands and compares their values -- numerically with their sign ignored and assumed to be 0. -- -- If, without signs, the first operand is the larger then the original -- first operand is returned (that is, with the original sign). If, -- without signs, the second operand is the larger then the original -- second operand is returned. Otherwise the result is the same as from -- the max operation. maxMagnitude :: (Precision p, Rounding r) => Decimal a b -> Decimal a b -> Arith p r (Decimal a b) -- | min takes two operands, compares their values numerically, and -- returns the minimum. If either operand is a NaN then the general rules -- apply, unless one is a quiet NaN and the other is numeric, in which -- case the numeric operand is returned. min :: (Precision p, Rounding r) => Decimal a b -> Decimal a b -> Arith p r (Decimal a b) -- | minMagnitude takes two operands and compares their values -- numerically with their sign ignored and assumed to be 0. -- -- If, without signs, the first operand is the smaller then the original -- first operand is returned (that is, with the original sign). If, -- without signs, the second operand is the smaller then the original -- second operand is returned. Otherwise the result is the same as from -- the min operation. minMagnitude :: (Precision p, Rounding r) => Decimal a b -> Decimal a b -> Arith p r (Decimal a b) -- | minus takes one operand, and corresponds to the prefix minus -- operator in programming languages. -- -- Note that the result of this operation is affected by context and may -- set flags. The copyNegate operation may be used instead -- of minus if this is not desired. minus :: (Precision p, Rounding r) => Decimal a b -> Arith p r (Decimal p r) -- | plus takes one operand, and corresponds to the prefix plus -- operator in programming languages. -- -- Note that the result of this operation is affected by context and may -- set flags. plus :: (Precision p, Rounding r) => Decimal a b -> Arith p r (Decimal p r) -- | multiply takes two operands. If either operand is a special -- value then the general rules apply. Otherwise, the operands are -- multiplied together (“long multiplication”), resulting in a number -- which may be as long as the sum of the lengths of the two operands. -- -- The result is then rounded to precision digits if necessary, -- counting from the most significant digit of the result. multiply :: (Precision p, Rounding r) => Decimal a b -> Decimal c d -> Arith p r (Decimal p r) -- | reduce takes one operand. It has the same semantics as the -- plus operation, except that if the final result is finite it is -- reduced to its simplest form, with all trailing zeros removed and its -- sign preserved. reduce :: (Precision p, Rounding r) => Decimal a b -> Arith p r (Decimal p r) -- | canonical takes one operand. The result has the same value as -- the operand but always uses a canonical encoding. The -- definition of canonical is implementation-defined; if more than -- one internal encoding for a given NaN, Infinity, or finite number is -- possible then one “preferred” encoding is deemed canonical. This -- operation then returns the value using that preferred encoding. -- -- If all possible operands have just one internal encoding each, then -- canonical always returns the operand unchanged (that is, it has -- the same effect as copy). This operation is unaffected by -- context and is quiet – no flags are changed in the context. canonical :: Decimal a b -> Arith p r (Decimal a b) -- | class_ takes one operand. The result is an indication of the -- class of the operand, where the class is one of ten -- possibilities, corresponding to one of the strings "sNaN" -- (signaling NaN), "NaN" (quiet NaN), "-Infinity" -- (negative infinity), "-Normal" (negative normal finite -- number), "-Subnormal" (negative subnormal finite number), -- "-Zero" (negative zero), "+Zero" (non-negative -- zero), "+Subnormal" (positive subnormal finite number), -- "+Normal" (positive normal finite number), or -- "+Infinity" (positive infinity). This operation is quiet; no -- flags are changed in the context. -- -- Note that unlike the special values in the model, the sign of any NaN -- is ignored in the classification, as required by IEEE 754. class_ :: Precision a => Decimal a b -> Arith p r Class data Class Class :: Sign -> Subclass -> Class data Sign -- | Positive or non-negative Pos :: Sign -- | Negative Neg :: Sign data Subclass -- | Zero ZeroClass :: Subclass -- | Normal finite number NormalClass :: Subclass -- | Subnormal finite number SubnormalClass :: Subclass -- | Infinity InfinityClass :: Subclass -- | Not a number (quiet or signaling) NaNClass :: Subclass -- | copy takes one operand. The result is a copy of the operand. -- This operation is unaffected by context and is quiet – no flags -- are changed in the context. copy :: Decimal a b -> Arith p r (Decimal a b) -- | copyAbs takes one operand. The result is a copy of the operand -- with the sign set to 0. Unlike the abs operation, this -- operation is unaffected by context and is quiet – no flags are -- changed in the context. copyAbs :: Decimal a b -> Arith p r (Decimal a b) -- | copyNegate takes one operand. The result is a copy of the -- operand with the sign inverted (a sign of 0 becomes 1 -- and vice versa). Unlike the minus operation, this operation is -- unaffected by context and is quiet – no flags are changed in -- the context. copyNegate :: Decimal a b -> Arith p r (Decimal a b) -- | copySign takes two operands. The result is a copy of the first -- operand with the sign set to be the same as the sign of -- the second operand. This operation is unaffected by context and is -- quiet – no flags are changed in the context. copySign :: Decimal a b -> Decimal c d -> Arith p r (Decimal a b) -- | isCanonical takes one operand. The result is 1 if the operand -- is canonical; otherwise it is 0. The definition of -- canonical is implementation-defined; if more than one internal -- encoding for a given NaN, Infinity, or finite number is possible then -- one “preferred” encoding is deemed canonical. This operation then -- tests whether the internal encoding is that preferred encoding. -- -- If all possible operands have just one internal encoding each, then -- isCanonical always returns 1. This operation is unaffected by -- context and is quiet – no flags are changed in the context. isCanonical :: Decimal a b -> Arith p r (Decimal p r) -- | isFinite takes one operand. The result is 1 if the operand is -- neither infinite nor a NaN (that is, it is a normal number, a -- subnormal number, or a zero); otherwise it is 0. This operation is -- unaffected by context and is quiet – no flags are changed in -- the context. isFinite :: Decimal a b -> Arith p r (Decimal p r) -- | isInfinite takes one operand. The result is 1 if the operand is -- an Infinity; otherwise it is 0. This operation is unaffected by -- context and is quiet – no flags are changed in the context. isInfinite :: Decimal a b -> Arith p r (Decimal p r) -- | isNaN takes one operand. The result is 1 if the operand is a -- NaN (quiet or signaling); otherwise it is 0. This operation is -- unaffected by context and is quiet – no flags are changed in -- the context. isNaN :: Decimal a b -> Arith p r (Decimal p r) -- | isNormal takes one operand. The result is 1 if the operand is a -- positive or negative normal number; otherwise it is 0. This -- operation is quiet; no flags are changed in the context. isNormal :: Precision a => Decimal a b -> Arith p r (Decimal p r) -- | isQNaN takes one operand. The result is 1 if the operand is a -- quiet NaN; otherwise it is 0. This operation is unaffected by context -- and is quiet – no flags are changed in the context. isQNaN :: Decimal a b -> Arith p r (Decimal p r) -- | isSigned takes one operand. The result is 1 if the sign -- of the operand is 1; otherwise it is 0. This operation is unaffected -- by context and is quiet – no flags are changed in the context. isSigned :: Decimal a b -> Arith p r (Decimal p r) -- | isSNaN takes one operand. The result is 1 if the operand is a -- signaling NaN; otherwise it is 0. This operation is unaffected by -- context and is quiet – no flags are changed in the context. isSNaN :: Decimal a b -> Arith p r (Decimal p r) -- | isSubnormal takes one operand. The result is 1 if the operand -- is a positive or negative subnormal number; otherwise it is 0. -- This operation is quiet; no flags are changed in the context. isSubnormal :: Precision a => Decimal a b -> Arith p r (Decimal p r) -- | isZero takes one operand. The result is 1 if the operand is a -- zero; otherwise it is 0. This operation is unaffected by context and -- is quiet – no flags are changed in the context. isZero :: Decimal a b -> Arith p r (Decimal p r) -- | radix takes no operands. The result is the radix (base) in -- which arithmetic is effected; for this specification the result will -- have the value 10. radix :: Precision p => Arith p r (Decimal p r) -- | sameQuantum takes two operands, and returns 1 if the two -- operands have the same exponent or 0 otherwise. The result is -- never affected by either the sign or the coefficient of either -- operand. -- -- If either operand is a special value, 1 is returned only if -- both operands are NaNs or both are infinities. -- -- sameQuantum does not change any flags in the context. sameQuantum :: Decimal a b -> Decimal c d -> Arith p r (Decimal p r) instance GHC.Classes.Eq Numeric.Decimal.Operation.Class instance GHC.Classes.Eq Numeric.Decimal.Operation.Subclass instance GHC.Show.Show Numeric.Decimal.Operation.Class -- | This module provides a general-purpose number type supporting decimal -- arithmetic for both limited precision floating-point (IEEE 754-2008) -- and for arbitrary precision floating-point (following the same -- principles as IEEE 754 and IEEE 854-1987) as described in the -- General Decimal Arithmetic Specification by Mike Cowlishaw. In -- addition to floating-point arithmetic, integer and unrounded -- floating-point arithmetic are included as subsets. -- -- Unlike the binary floating-point types Float and Double, -- decimal number types can perform decimal arithmetic exactly. -- Internally, decimal numbers are represented with an integral -- coefficient and base-10 exponent. -- --
--   >>> 29.99 + 4.71 :: Double
--   34.699999999999996
--   
-- --
--   >>> 29.99 + 4.71 :: BasicDecimal
--   34.70
--   
-- --
--   >>> 0.1 + 0.2 == (0.3 :: Double)
--   False
--   
-- --
--   >>> 0.1 + 0.2 == (0.3 :: BasicDecimal)
--   True
--   
-- -- Decimal numbers support lossless conversion to and from a string -- representation via Show and Read instances. Note that -- there may be multiple representations of values that are numerically -- equal (e.g. 1 and 1.00) which are preserved by this conversion. module Numeric.Decimal -- | A decimal floating point number with selectable precision and rounding -- algorithm data Decimal p r -- | A decimal floating point number with 9 digits of precision, rounding -- half up type BasicDecimal = Decimal P9 RoundHalfUp -- | A decimal floating point number with selectable precision, rounding -- half even type ExtendedDecimal p = Decimal p RoundHalfEven -- | A decimal floating point number with infinite precision type GeneralDecimal = ExtendedDecimal PInfinite -- | Precision indicates the maximum number of significant decimal digits a -- number may have. class Precision p -- | Return the precision of the argument, or Nothing if the -- precision is infinite. precision :: Precision p => p -> Maybe Int -- | A subclass of precisions that are finite class Precision p => FinitePrecision p -- | A precision of 1 significant digit data P1 -- | A precision of 2 significant digits type P2 = PPlus1 P1 -- | A precision of 3 significant digits type P3 = PPlus1 P2 -- | Et cetera type P4 = PTimes2 P2 type P5 = PPlus1 P4 type P6 = PTimes2 P3 type P7 = PPlus1 P6 type P8 = PTimes2 P4 type P9 = PPlus1 P8 type P10 = PTimes2 P5 type P11 = PPlus1 P10 type P12 = PTimes2 P6 type P13 = PPlus1 P12 type P14 = PTimes2 P7 type P15 = PPlus1 P14 type P16 = PTimes2 P8 type P17 = PPlus1 P16 type P18 = PTimes2 P9 type P19 = PPlus1 P18 type P20 = PTimes2 P10 type P21 = PPlus1 P20 type P22 = PTimes2 P11 type P23 = PPlus1 P22 type P24 = PTimes2 P12 type P25 = PPlus1 P24 type P26 = PTimes2 P13 type P27 = PPlus1 P26 type P28 = PTimes2 P14 type P29 = PPlus1 P28 type P30 = PTimes2 P15 type P31 = PPlus1 P30 type P32 = PTimes2 P16 type P33 = PPlus1 P32 type P34 = PTimes2 P17 type P35 = PPlus1 P34 type P36 = PTimes2 P18 type P37 = PPlus1 P36 type P38 = PTimes2 P19 type P39 = PPlus1 P38 type P40 = PTimes2 P20 type P41 = PPlus1 P40 type P42 = PTimes2 P21 type P43 = PPlus1 P42 type P44 = PTimes2 P22 type P45 = PPlus1 P44 type P46 = PTimes2 P23 type P47 = PPlus1 P46 type P48 = PTimes2 P24 type P49 = PPlus1 P48 type P50 = PTimes2 P25 type P75 = PPlus5 (PTimes10 P7) type P100 = PTimes2 P50 type P150 = PTimes2 P75 type P200 = PTimes2 P100 type P250 = PTimes10 P25 type P300 = PTimes2 P150 type P400 = PTimes2 P200 type P500 = PTimes2 P250 type P1000 = PTimes2 P500 type P2000 = PTimes2 P1000 type PPlus1 p = PPlus p P1 A precision of (@p@ + 1) significant digits type PPlus2 p = PPlus p P2 A precision of (@p@ + 2) significant digits type PPlus3 p = PPlus p P3 A precision of (@p@ + 3) significant digits type PPlus4 p = PPlus p P4 A precision of (@p@ + 4) significant digits type PPlus5 p = PPlus p P5 A precision of (@p@ + 5) significant digits type PPlus6 p = PPlus p P6 A precision of (@p@ + 6) significant digits type PPlus7 p = PPlus p P7 A precision of (@p@ + 7) significant digits type PPlus8 p = PPlus p P8 A precision of (@p@ + 8) significant digits type PPlus9 p = PPlus p P9 A precision of (@p@ + 9) significant digits type PTimes2 = PTimes P2 type PTimes10 = PTimes P10 -- | A precision of unlimited significant digits data PInfinite -- | A rounding algorithm to use when the result of an arithmetic operation -- exceeds the precision of the result type class Rounding r -- | If the discarded digits represent greater than or equal to half (0.5) -- of the value of a one in the next left position then the value is -- rounded up. If they represent less than half, the value is rounded -- down. data RoundHalfUp -- | If the discarded digits represent greater than half (0.5) of the value -- of a one in the next left position then the value is rounded up. If -- they represent less than half, the value is rounded down. If they -- represent exactly half, the value is rounded to make its rightmost -- digit even. data RoundHalfEven -- | If the discarded digits represent greater than half (0.5) of the value -- of a one in the next left position then the value is rounded up. If -- they represent less than half or exactly half, the value is rounded -- down. data RoundHalfDown -- | Round toward +∞ data RoundCeiling -- | Round toward −∞ data RoundFloor -- | Round away from 0 data RoundUp -- | Round zero or five away from 0 data Round05Up -- | Round toward 0 (truncate) data RoundDown -- | Cast a Decimal to another precision and/or rounding algorithm, -- immediately rounding if necessary to the new precision using the new -- algorithm. cast :: (Precision p, Rounding r) => Decimal a b -> Decimal p r -- | Return False if the argument is zero or NaN, and True -- otherwise. toBool :: Decimal p r -> Bool