-- Hoogle documentation, generated by Haddock -- See Hoogle, http://www.haskell.org/hoogle/ -- | Decimal floating point arithmetic -- -- deka provides decimal floating point arithmetic. It is based on -- mpdecimal, the C library used to provide support for the Decimal -- module in Python 3. -- -- You will need to install mpdecimal to use deka; otherwise your -- executables will not link. It is available at -- -- http://www.bytereef.org/mpdecimal/ -- -- mpdecimal has also been packaged for some Linux distributions, such as -- Debian (libmpdec-dev - available in Jessie and later) and Arch -- (mpdecimal). -- -- mpdecimal, in turn, implements the General Decimal Arithmetic -- Specification, which is available at -- -- http://speleotrove.com/decimal/ -- -- For more on deka, please see the Github home page at -- -- https://github.com/massysett/deka -- -- Tests are packaged separately in the deka-tests package. @package deka @version 0.6.0.2 -- | Documentation for Deka. -- -- At the moment, documentation is scattered about. Some of it is in the -- main README.md, which is in the source code tree and is viewable in -- Github at -- -- http://github.com/massysett/deka/blob/master/README.md -- -- Of course much of it is in the Haddock comments in the source code -- itself. -- -- There is also a module here, Deka.Docs.Examples. It is in -- literate Haskell and has many comments. Unfortunately Haddock does not -- play well with Literate Haskell. However, the style of the file would -- not play well with Haddock anyway so I'm not sure I would ever switch -- back to regular Haskell for that file. -- -- So if you link to the file from the Haddock docs, you will just get a -- blank page. Fortunately it is easily readable in Github: -- -- -- http://github.com/massysett/deka/blob/master/lib/Deka/Docs/Examples.hs module Deka.Docs module Deka.Context -- | A signed integer. Its size is platform dependent. type Signed = Int64 -- | The Ctx monad -- -- The General Decimal Arithmetic specification states that most -- computations occur within a context, which affects the manner -- in which computations are done (for instance, the context determines -- the rounding algorithm). The context also carries the flags that -- computations can set (for instance, a computation might set a flag to -- indicate that the result is rounded or inexact or was a division by -- zero.) The Ctx monad carries this context. data Ctx a -- | Indicates error conditions. This type serves two purposes: -- computations set flags to indicate errors, and flags indicate which -- errors you want to have raise a signal. See getStatus, -- setStatus, getTraps, and setTraps. -- -- Flag is an instance of Exception so that you can throw -- it if you want; however, none of the functions in the deka -- package throw. data Flag -- | A container of Flag. data Flags -- | A list of all possible Flag, in order. allFlag :: [Flag] -- | All possible Flag are set. fullFlags :: Flags -- | No Flag are set. emptyFlags :: Flags packFlags :: [Flag] -> Flags -- | Flags will always be unpacked in order. unpackFlags :: Flags -> [Flag] clamped :: Flag -- | A source string (for instance, in fromByteString) contained -- errors. conversionSyntax :: Flag -- | A non-zero dividend is divided by zero. Unlike 0/0, it has a -- defined result (a signed Infinity). divisionByZero :: Flag -- | Sometimes raised by divideInteger and remainder. divisionImpossible :: Flag -- | 0/0 is undefined. It sets this flag and returns a quiet NaN. divisionUndefined :: Flag fpuError :: Flag -- | One or more non-zero coefficient digits were discarded during -- rounding. inexact :: Flag -- | The Context for computations was invalid; this error should never -- occur because deka keeps you from setting an invalid context. invalidContext :: Flag -- | Raised on a variety of invalid operations, such as an attempt to use -- compareSignal on an operand that is an NaN. invalidOperation :: Flag mallocError :: Flag notImplemented :: Flag -- | The exponent of a result is too large to be represented. overflow :: Flag rounded :: Flag subnormal :: Flag -- | A result is both subnormal and inexact. underflow :: Flag -- | Gets all currently set traps. getTraps :: Ctx Flags -- | If you set a trap, a computation will immediately raise -- SIGFPE if the corresponding error arises. (Currently this -- behavior cannot be configured to do something else.) setTraps -- clears all existing traps and sets them to the new ones you specify. -- -- By setting a flag here, SIGFPE is raised if any subsequent -- computations raise the corresponding error condition. Setting a flag -- with this function or with setTrap never, by itself, causes -- SIGFPE to be raised; it is raised only by a subsequent computation. -- So, if you set a flag using this function or setTrap and the -- corresponding status flag is already set, SIGFPE will be raised only -- if a subsequent computation raises that error condition. setTraps :: Flags -> Ctx () -- | All currently set status flags. getStatus :: Ctx Flags -- | Sets status flags. All existing status flags are cleared and replaced -- with the ones you indicate here. setStatus :: Flags -> Ctx () -- | Sets the precision to be used for all operations. The result of an -- operation is rounded to this length if necessary. data Precision -- | Creates a Precision that you can then set with setTrio. -- Returns Nothing if the argument is out of range. The minimum -- possible value is always 1; the maximum possible value is platform -- dependent and is revealed by maxBound. precision :: Signed -> Maybe Precision unPrecision :: Precision -> Signed getPrecision :: Ctx Precision -- | Sets the precision to the maximum possible, respecting that -- Emax > 5 * Precision. Returns the new -- Precision. setMaxPrecision :: Ctx Precision data Round -- | Round toward positive infinity. roundCeiling :: Round -- | Round away from zero. roundUp :: Round -- | 0.5 rounds up roundHalfUp :: Round -- | 0.5 rounds to nearest even roundHalfEven :: Round -- | 0.5 rounds down roundHalfDown :: Round -- | Round toward zero - truncate roundDown :: Round -- | Round toward negative infinity. roundFloor :: Round -- | Round for reround round05Up :: Round -- | Truncate, but set infinities. roundTruncate :: Round getRound :: Ctx Round setRound :: Round -> Ctx () -- | Maximum adjusted exponent. The adjusted exponent is calculated as -- though the number were expressed in scientific notation. If the -- adjusted exponent would be larger than Emax then an overflow -- results. -- -- The minimum possible value is always 0; the maximum possible value is -- platform dependent and is revealed by maxBound. data Emax unEmax :: Emax -> Signed -- | Returns an Emax for use in setTrio. Fails if argument is -- out of range. emax :: Signed -> Maybe Emax getEmax :: Ctx Emax -- | Minimum adjusted exponent. The adjusted exponent is calculated as -- though the number were expressed in scientific notation. If the -- adjusted exponent would be smaller than Emin then the result is -- subnormal. If the result is also inexact, an underflow results. If -- subnormal results are allowed (see setClamp) the smallest -- possible exponent is Emin minus Precision plus -- 1. -- -- The minimum possible value is platform dependent and is revealed by -- minBound; the maximum possible value is always 0. data Emin unEmin :: Emin -> Signed -- | Returns an Emin for use in setTrio. Fails if argument is -- out of range. emin :: Signed -> Maybe Emin getEmin :: Ctx Emin -- | In addition to the limits on Precision, Emax, and -- Emin, there are also requirements on the relationship between -- these three variables: -- --
-- -- The Trio enforces this relationship. -- -- It is also recommended that Emax > 10 * -- Precision, but since this is not required the Trio -- does not enforce it. data Trio trioPrecision :: Trio -> Precision trioEmax :: Trio -> Emax trioEmin :: Trio -> Emin -- | Make a new Trio. Fails if the values are out of range. trio :: Precision -> Emax -> Emin -> Maybe Trio setTrio :: Trio -> Ctx () getTrio :: Ctx Trio getClamp :: Ctx Bool -- | Controls explicit exponent clamping. When False, a result exponent is -- limited to a maximum of emax and a minimum of emin (for example, the -- exponent of a zero result will be clamped to be in this range). When -- True, a result exponent has the same minimum but is limited to a -- maximum of emax-(digits-1). As well as clamping zeros, this may cause -- the coefficient of a result to be padded with zeros on the right in -- order to bring the exponent within range. -- -- Also when True, this limits the length of NaN payloads to -- Precision - 1 when constructing a NaN by conversion from a -- string. setClamp :: Bool -> Ctx () -- | By default, most functions are correctly rounded. By setting -- allCorrectRound, correct rounding is additionally enabled for exp, ln, -- and log10. In this case, all functions except pow and invroot return -- correctly rounded results. getAllCorrectRound :: Ctx Bool setAllCorrectRound :: Bool -> Ctx () -- | Before running computations in a context. the context must be -- initialized with certain settings, such as the rounding mode, -- precision, and maximum adjusted exponent. An Initializer -- contains all these settings. -- -- On 64-bit platforms, the maximums are: -- ---- (-1) ^ sign * coefficient * 10 ^ exponent ---- -- In addition to finite numbers, a number may also be one of three -- special values: -- --
-- -NaN < -sNaN < -Infinity < -finites < -0 < +0 < +finites -- < +Infinity < +SNaN < +NaN ---- -- Also, 1.000 < 1.0 (etc.) and NaNs are ordered by -- payload. compareTotal :: Dec -> Dec -> Ordering -- | Same as compareTotal except that the signs of the operands are -- ignored and taken to be 0 (non-negative). compareTotalMag :: Dec -> Dec -> Ordering -- | Compares two numbers numerically and returns the larger. If the -- numbers compare equal then number is chosen with regard to sign and -- exponent. Unusually, if one operand is a quiet NaN and the other a -- number, then the number is returned. max :: Dec -> Dec -> Ctx Dec -- | Compares the magnitude of two numbers numerically and sets number to -- the larger. It is identical to max except that the signs of the -- operands are ignored and taken to be 0 (non-negative). maxMag :: Dec -> Dec -> Ctx Dec -- | Compares two numbers numerically and sets number to the smaller. If -- the numbers compare equal then number is chosen with regard to sign -- and exponent. Unusually, if one operand is a quiet NaN and the other a -- number, then the number is returned. min :: Dec -> Dec -> Ctx Dec -- | Compares the magnitude of two numbers numerically and sets number to -- the smaller. It is identical to min except that the signs of -- the operands are ignored and taken to be 0 (non-negative). minMag :: Dec -> Dec -> Ctx Dec -- | Returns the closest representable number that is smaller than the -- operand. nextMinus :: Dec -> Ctx Dec -- | Returns the closest representable number that is larger than the -- operand. nextPlus :: Dec -> Ctx Dec -- | nextToward a b returns the representable number closest to -- a in the direction of b. nextToward :: Dec -> Dec -> Ctx Dec -- | True if both operands have the same exponent; False otherwise. sameQuantum :: Dec -> Dec -> Bool -- | quantize a b returns the number that is equal in value to -- a, but has the exponent of b. quantize :: Dec -> Dec -> Ctx Dec -- | rescale a b returns the number that is equal in value to -- a, but has the exponent b. Special numbers are -- copied without signaling. This function is not part of the General -- Decimal Arithmetic Specification. It is also not equivalent to the -- rescale function that was removed from the specification. rescale :: Dec -> Signed -> Ctx Dec -- | scaleB a b - b must be an integer with exponent 0. If -- a is infinite, returns a. Otherwise, returns -- a with the value of b added to the exponent. scaleB :: Dec -> Dec -> Ctx Dec -- | Digit-wise logical and. and :: Dec -> Dec -> Ctx Dec -- | Digit-wise logical inclusive or. or :: Dec -> Dec -> Ctx Dec -- | Digit-wise logical exclusive or. xor :: Dec -> Dec -> Ctx Dec -- | shift a b returns a shifted by b places. -- b must be in the range [-Precision, Precision]. -- A negative b indicates a right shift, a positive b a -- left shift. Digits that do not fit are discarded. shift :: Dec -> Dec -> Ctx Dec -- | rotate x y returns x rotated by y places. -- y must be in the range [-Precision, Precision]. -- A negative y indicates a right rotation, a positive -- y a left rotation. rotate :: Dec -> Dec -> Ctx Dec -- | Digit-wise inversion (a 0 becomes a 1 and vice -- versa). invert :: Dec -> Ctx Dec reduce :: Dec -> Ctx Dec -- | Round to an integer, using the rounding mode of the context. Only a -- signaling NaN causes an invalidOperation condition. toIntegralExact :: Dec -> Ctx Dec -- | Like toIntegralExact, but inexact and rounded are -- never set. toIntegralValue :: Dec -> Ctx Dec -- | Exponentiation. Result is rounded if necessary using the -- Precision in the Ctx and using the roundHalfEven -- rounding method. -- -- Finite results will always be full precision and inexact, except when -- rhs is a zero or -Infinity (giving 1 or 0 respectively). Inexact -- results will almost always be correctly rounded, but may be up to 1 -- ulp (unit in last place) in error in rare cases. -- -- This is a mathematical function; the 10 ^ 6 restrictions on -- precision and range apply as described above. exp :: Dec -> Ctx Dec -- | Natural logarithm. Results are correctly rounded if -- setAllCorrectRound is True. ln :: Dec -> Ctx Dec -- | Returns the adjusted exponent of the operand, according to the rules -- for logB of IEEE 754. This returns the exponent of the -- operand as though its decimal point had been moved to follow the first -- digit while keeping the same value. The result is not limited by -- Emin or Emax. -- -- If operand is an NaN, the general rules apply. If operand is infinite, -- the result is +Infinity. If operand is zero, result is -Infinity and -- invalidOperation is set. Otherwise, the result is the same as -- the adjusted exponent of the operand, or floor(log10(a)) -- where a is the operand. logB :: Dec -> Ctx Dec -- | Base 10 logarithm. Results are correctly rounded if -- setAllCorrectRound is True. log10 :: Dec -> Ctx Dec -- | power b e returns b raised to the power of -- e. Integer powers are exact, provided that the result is -- finite and fits into Precision. -- -- Results are not correctly rounded, even if setAllCorrectRound -- is True. The error of the function is less than 1ULP + t, -- where t has a maximum of 0.1ULP, but is almost -- always less than 0.001ULP. power :: Dec -> Dec -> Ctx Dec -- | Returns the square root. This function is always correctly rounded -- using the roundHalfEven method. squareRoot :: Dec -> Ctx Dec data PosNeg Pos :: PosNeg Neg :: PosNeg data Number Infinity :: Number Normal :: Number Subnormal :: Number Zero :: Number data Class SNaN :: Class NaN :: Class Number :: PosNeg -> Number -> Class strToClass :: IsString a => [(a, Class)] -- | Determines the Class of a Dec. numClass :: Dec -> Ctx Class -- | False if the decimal is special or zero, or the exponent is less than -- Emin. True otherwise. isNormal :: Dec -> Ctx Bool -- | False if the decimal is special or zero, or the exponent is greater or -- equal to Emin. True otherwise. isSubnormal :: Dec -> Ctx Bool isFinite :: Dec -> Bool isInfinite :: Dec -> Bool isNaN :: Dec -> Bool isNegative :: Dec -> Bool isPositive :: Dec -> Bool isSigned :: Dec -> Bool isQNaN :: Dec -> Bool isSNaN :: Dec -> Bool isSpecial :: Dec -> Bool isZero :: Dec -> Bool isZeroCoeff :: Dec -> Bool isOddCoeff :: Dec -> Bool -- | The sign of a number. data Sign -- | A sign of zero; used for positive numbers and for zero. Sign0 :: Sign -- | A sign of one; used for negative numbers and the negative zero. Sign1 :: Sign sign :: Dec -> Sign data EvenOdd Even :: EvenOdd Odd :: EvenOdd evenOdd :: Dec -> (Maybe EvenOdd) version :: ByteString instance [safe] Show Dec module Deka.Native.Abstract -- | A digit from one to nine. Useful to represent a most significant -- digit, or MSD, as an MSD cannot be the digit zero. data Novem D1 :: Novem D2 :: Novem D3 :: Novem D4 :: Novem D5 :: Novem D6 :: Novem D7 :: Novem D8 :: Novem D9 :: Novem novemToChar :: Novem -> Char charToNovem :: Char -> Maybe Novem novemToInt :: Integral a => Novem -> a intToNovem :: Integral a => a -> Maybe Novem -- | A digit from zero to nine. data Decem D0 :: Decem Nonem :: Novem -> Decem decemToChar :: Decem -> Char charToDecem :: Char -> Maybe Decem decemToInt :: Integral a => Decem -> a decemToNovem :: Decem -> Maybe Novem intToDecem :: Integral a => a -> Maybe Decem intToDecemList :: Integral a => a -> (Sign, [Decem]) decemListToInt :: Integral a => [Decem] -> a -- | A non-empty set of digits. The MSD must be from 1 to 9. data Decuple Decuple :: Novem -> [Decem] -> Decuple decupleToString :: Decuple -> String stringToDecuple :: String -> Maybe Decuple decupleToInt :: Integral a => Decuple -> a uncons :: [a] -> Maybe (a, [a]) intToDecuple :: Integral a => a -> Maybe (Sign, Decuple) decemListToDecuple :: [Decem] -> Maybe Decuple -- | Either a set of digits, or zero. Unsigned. data Aut -- | Zero Nil :: Aut -- | Non-zero Plenus :: Decuple -> Aut autToString :: Aut -> String stringToAut :: String -> Maybe Aut autToInt :: Integral a => Aut -> a -- | Fails if the argument is less than zero. intToAut :: Integral a => a -> Maybe Aut decemListToAut :: [Decem] -> Aut -- | Either a set of digits, or zero. Signed. data Firmado -- | Zero Cero :: Firmado -- | Non-zero Completo :: PosNeg -> Decuple -> Firmado firmadoToString :: Firmado -> String stringToFirmado :: String -> Maybe Firmado firmadoToInt :: Integral a => Firmado -> a intToFirmado :: Integral a => a -> Firmado -- | The coefficient in a number; not used in infinities or NaNs. newtype Coefficient Coefficient :: Aut -> Coefficient unCoefficient :: Coefficient -> Aut -- | The exponent in a number. newtype Exponent Exponent :: Firmado -> Exponent unExponent :: Exponent -> Firmado -- | The diagnostic information in an NaN. newtype Diagnostic Diagnostic :: Decuple -> Diagnostic unDiagnostic :: Diagnostic -> Decuple -- | Whether an NaN is quiet or signaling. data Noisy Quiet :: Noisy Signaling :: Noisy -- | Not a Number. data NonNum NonNum :: Noisy -> Maybe Diagnostic -> NonNum noisy :: NonNum -> Noisy diagnostic :: NonNum -> Maybe Diagnostic -- | All data in an abstract number except for the sign. data Value Finite :: Coefficient -> Exponent -> Value Infinite :: Value NotANumber :: NonNum -> Value -- | Abstract representation of all numbers covered by the General Decimal -- Arithmetic Specification. data Abstract Abstract :: Sign -> Value -> Abstract sign :: Abstract -> Sign value :: Abstract -> Value signToString :: Sign -> String -- | Adjusted exponent. Roughly speaking this represents the coefficient -- and exponent of an abstract decimal, adjusted so there is a decimal -- point between the most significant digit of the coefficient and the -- remaning digits. newtype AdjustedExp AdjustedExp :: Integer -> AdjustedExp unAdjustedExp :: AdjustedExp -> Integer -- | Computes an adjusted exponent. The length of a zero coefficient is -- one. adjustedExp :: Coefficient -> Exponent -> AdjustedExp fmtAdjustedExp :: AdjustedExp -> String finiteToString :: Coefficient -> Exponent -> String nanToString :: NonNum -> String fmtValue :: Value -> String -- | Transform an Abstract to a String. This conforms to the -- to-scientific-string transformation given in the General -- Decimal Arithmetic Specification at -- -- http://speleotrove.com/decimal/daconvs.html#reftostr -- -- with one exception: the specification provides that some finite -- numbers are represented without exponential notation. -- abstractToString always uses exponential notation on -- finite numbers. abstractToString :: Abstract -> String -- | Transforms an Abstract to a Dec. Result is computed in a -- context using the Pedantic initializer. Result is returned -- along with any status flags arising from the computation. abstractToDec :: Abstract -> (Dec, Flags) instance Eq Novem instance Ord Novem instance Show Novem instance Enum Novem instance Bounded Novem instance Eq Decem instance Ord Decem instance Show Decem instance Eq Decuple instance Ord Decuple instance Show Decuple instance Eq Aut instance Ord Aut instance Show Aut instance Eq Firmado instance Ord Firmado instance Show Firmado instance Eq Coefficient instance Ord Coefficient instance Show Coefficient instance Eq Exponent instance Ord Exponent instance Show Exponent instance Eq Diagnostic instance Ord Diagnostic instance Show Diagnostic instance Eq Noisy instance Ord Noisy instance Show Noisy instance Eq NonNum instance Ord NonNum instance Show NonNum instance Eq Value instance Ord Value instance Show Value instance Eq Abstract instance Ord Abstract instance Show Abstract instance Eq AdjustedExp instance Ord AdjustedExp instance Show AdjustedExp -- | Uses the specification for string conversions given in the General -- Decimal Arithmetic Specification to convert strings to an abstract -- syntax tree. The specification for string conversions is at -- -- http://speleotrove.com/decimal/daconvs.html -- -- The functions and types in this module fall into two groups. The first -- group converts a string to a NumericString, which is an -- abstract representation of the grammar given in the General Decimal -- Arithmetic Specification. These functions use Parsec to parse the -- string. The second group transforms the NumericString to an -- Abstract, a form which more closely aligns with the abstract -- representation given at -- -- http://speleotrove.com/decimal/damodel.html. -- -- You can transform an Abstract to a numeric string; no functions -- are provided to transform a NumericString directly back to a -- string. module Deka.Native.FromString sign :: Parser Sign optSign :: Parser Sign digit :: Parser Decem indicator :: Parser () digits :: Parser [Decem] data DecimalPart WholeFrac :: [Decem] -> [Decem] -> DecimalPart WholeOnly :: [Decem] -> DecimalPart decimalPart :: Parser DecimalPart data ExponentPart ExponentPart :: Sign -> [Decem] -> ExponentPart expSign :: ExponentPart -> Sign expDigits :: ExponentPart -> [Decem] exponentPart :: Parser ExponentPart infinity :: Parser () nanId :: Parser Noisy data NaN NaN :: Noisy -> [Decem] -> NaN nan :: Parser NaN data NumericValue NVDec :: DecimalPart -> (Maybe ExponentPart) -> NumericValue Infinity :: NumericValue numericValue :: Parser NumericValue data NumericString NumericString :: Sign -> Either NumericValue NaN -> NumericString nsSign :: NumericString -> Sign nsValue :: NumericString -> Either NumericValue NaN numericString :: Parser NumericString parseNumericString :: String -> Either String NumericString numericStringToAbstract :: NumericString -> Abstract nanToAbstract :: NaN -> NonNum finiteToAbstract :: DecimalPart -> Maybe ExponentPart -> (Coefficient, Exponent) -- | A numeric value for the exponent that was given in the input string. givenExponent :: Maybe ExponentPart -> Integer -- | The number of digits after the decimal point, subtracted from the -- numeric value for the exponent given in the string actualExponent :: DecimalPart -> Integer -> Integer abstractExponent :: Integer -> Exponent abstractCoeff :: DecimalPart -> Coefficient stringToAbstract :: String -> Either String Abstract -- | Transforms a Dec to an Abstract. decToAbstract :: Dec -> Abstract instance Eq DecimalPart instance Ord DecimalPart instance Show DecimalPart instance Eq ExponentPart instance Ord ExponentPart instance Show ExponentPart instance Eq NaN instance Ord NaN instance Show NaN instance Eq NumericValue instance Ord NumericValue instance Show NumericValue instance Eq NumericString instance Ord NumericString instance Show NumericString -- | Representation of numbers in native Haskell types. -- -- Since deka is a binding to the mpdecimal C library, the data types are -- held as pointers to data which are managed by C functions. Therefore -- there is no direct access to what is inside of the the Dec data -- type. Modules in Deka.Native provide Haskell types mirroring -- the abstract representations given in the General Decimal Arithmetic -- Specification. This is useful if you want to manipulate the data in an -- abstract way. For example, perhaps you want to perform arithmetic on a -- value, transform it to abstract form, add digit grouping characters, -- and then use your own functions to pretty print the result. -- -- The General Decimal Arithmetic Specification gives an abstract -- representation of each number. This information is taken from the -- General Decimal Arithmetic specification at -- -- http://speleotrove.com/decimal/damodel.html -- -- A number may be finite, in which case it has three components: -- a sign, which must be zero (for zero or positive numbers) or -- one (for negative zero and negative numbers), an integral -- coefficient, which is always zero or positive, and a signed -- integral exponent, which indicates the power of ten by which -- the number is multiplied. The value of a finite number if given by -- --
-- (-1) ^ sign * coefficient * 10 ^ exponent ---- -- In addition to finite numbers, a number may also be one of three -- special values: -- --