| Safe Haskell | Safe |
|---|
Data.Deka.Quad.IO
- newtype Ctx a = Ctx {
- unCtx :: Ptr C'decContext -> IO a
- newtype Quad = Quad {}
- newtype Round = Round {}
- roundCeiling :: Round
- roundUp :: Round
- roundHalfUp :: Round
- roundHalfEven :: Round
- roundHalfDown :: Round
- roundDown :: Round
- roundFloor :: Round
- round05Up :: Round
- newtype Flag = Flag C'uint32_t
- divisionUndefined :: Flag
- divisionByZero :: Flag
- divisionImpossible :: Flag
- invalidOperation :: Flag
- inexact :: Flag
- underflow :: Flag
- overflow :: Flag
- conversionSyntax :: Flag
- newtype Flags = Flags {}
- setFlag :: Flag -> Flags -> Flags
- clearFlag :: Flag -> Flags -> Flags
- checkFlag :: Flag -> Flags -> Bool
- emptyFlags :: Flags
- flagList :: Flags -> [String]
- getStatus :: Ctx Flags
- setStatus :: Flags -> Ctx ()
- getRound :: Ctx Round
- setRound :: Round -> Ctx ()
- runCtx :: Ctx a -> IO (a, Flags)
- evalCtx :: Ctx a -> IO a
- newtype DecClass = DecClass C'decClass
- sNan :: DecClass
- qNan :: DecClass
- negInf :: DecClass
- negNormal :: DecClass
- negSubnormal :: DecClass
- negZero :: DecClass
- posZero :: DecClass
- posSubnormal :: DecClass
- posNormal :: DecClass
- posInf :: DecClass
- newQuad :: IO Quad
- type MkString = Ptr C'decQuad -> CString -> IO CString
- mkString :: MkString -> Quad -> IO ByteString
- type Unary = Ptr C'decQuad -> Ptr C'decQuad -> Ptr C'decContext -> IO (Ptr C'decQuad)
- unary :: Unary -> Quad -> Ctx Quad
- type Binary = Ptr C'decQuad -> Ptr C'decQuad -> Ptr C'decQuad -> Ptr C'decContext -> IO (Ptr C'decQuad)
- binary :: Binary -> Quad -> Quad -> Ctx Quad
- type BinaryCtxFree = Ptr C'decQuad -> Ptr C'decQuad -> Ptr C'decQuad -> IO (Ptr C'decQuad)
- binaryCtxFree :: BinaryCtxFree -> Quad -> Quad -> IO Quad
- type UnaryGet a = Ptr C'decQuad -> IO a
- unaryGet :: UnaryGet a -> Quad -> IO a
- type Ternary = Ptr C'decQuad -> Ptr C'decQuad -> Ptr C'decQuad -> Ptr C'decQuad -> Ptr C'decContext -> IO (Ptr C'decQuad)
- ternary :: Ternary -> Quad -> Quad -> Quad -> Ctx Quad
- type Boolean = Ptr C'decQuad -> IO C'uint32_t
- boolean :: Boolean -> Quad -> IO Bool
- type GetRounded a = Ptr C'decQuad -> Ptr C'decContext -> C'rounding -> IO a
- getRounded :: GetRounded a -> Round -> Quad -> Ctx a
- abs :: Quad -> Ctx Quad
- add :: Quad -> Quad -> Ctx Quad
- and :: Quad -> Quad -> Ctx Quad
- decClass :: Quad -> IO DecClass
- compare :: Quad -> Quad -> Ctx Quad
- compareSignal :: Quad -> Quad -> Ctx Quad
- compareTotal :: Quad -> Quad -> IO Quad
- compareTotalMag :: Quad -> Quad -> IO Quad
- copySign :: Quad -> Quad -> IO Quad
- digits :: Quad -> IO Int
- divide :: Quad -> Quad -> Ctx Quad
- divideInteger :: Quad -> Quad -> Ctx Quad
- fma :: Quad -> Quad -> Quad -> Ctx Quad
- fromInt32 :: C'int32_t -> IO Quad
- fromByteString :: ByteString -> Ctx Quad
- fromUInt32 :: C'uint32_t -> IO Quad
- invert :: Quad -> Ctx Quad
- isFinite :: Quad -> IO Bool
- isInfinite :: Quad -> IO Bool
- isInteger :: Quad -> IO Bool
- isLogical :: Quad -> IO Bool
- isNaN :: Quad -> IO Bool
- isNegative :: Quad -> IO Bool
- isNormal :: Quad -> IO Bool
- isPositive :: Quad -> IO Bool
- isSignaling :: Quad -> IO Bool
- isSigned :: Quad -> IO Bool
- isSubnormal :: Quad -> IO Bool
- isZero :: Quad -> IO Bool
- logB :: Quad -> Ctx Quad
- max :: Quad -> Quad -> Ctx Quad
- maxMag :: Quad -> Quad -> Ctx Quad
- min :: Quad -> Quad -> Ctx Quad
- minMag :: Quad -> Quad -> Ctx Quad
- minus :: Quad -> Ctx Quad
- multiply :: Quad -> Quad -> Ctx Quad
- nextMinus :: Quad -> Ctx Quad
- nextPlus :: Quad -> Ctx Quad
- nextToward :: Quad -> Quad -> Ctx Quad
- or :: Quad -> Quad -> Ctx Quad
- plus :: Quad -> Ctx Quad
- quantize :: Quad -> Quad -> Ctx Quad
- reduce :: Quad -> Ctx Quad
- remainder :: Quad -> Quad -> Ctx Quad
- remainderNear :: Quad -> Quad -> Ctx Quad
- rotate :: Quad -> Quad -> Ctx Quad
- sameQuantum :: Quad -> Quad -> IO Bool
- scaleB :: Quad -> Quad -> Ctx Quad
- shift :: Quad -> Quad -> Ctx Quad
- subtract :: Quad -> Quad -> Ctx Quad
- toByteString :: Quad -> IO ByteString
- toEngByteString :: Quad -> IO ByteString
- toInt32 :: Round -> Quad -> Ctx C'int32_t
- toInt32Exact :: Round -> Quad -> Ctx C'int32_t
- toIntegralExact :: Quad -> Ctx Quad
- toIntegralValue :: Round -> Quad -> Ctx Quad
- toUInt32 :: Round -> Quad -> Ctx C'uint32_t
- toUInt32Exact :: Round -> Quad -> Ctx C'uint32_t
- version :: IO ByteString
- xor :: Quad -> Quad -> Ctx Quad
- zero :: IO Quad
- data Sign
- data NaN
- minMaxExp :: (Int, Int)
- minNormalAdj :: AdjustedExp
- minNormalExp :: Coefficient -> Exponent
- newtype Exponent = Exponent {
- unExponent :: Int
- exponent :: Int -> Maybe Exponent
- zeroExponent :: Exponent
- data Value
- data Decoded = Decoded {}
- toBCD :: Quad -> IO Decoded
- fromBCD :: Decoded -> IO Quad
- toDecNumberBCD :: Decoded -> (C'int32_t, [C'uint8_t], C'int32_t)
- getDecoded :: C'int32_t -> C'int32_t -> [C'uint8_t] -> Decoded
- scientific :: Decoded -> String
- sciFinite :: Coefficient -> Exponent -> String
- sciNaN :: NaN -> Payload -> String
- ordinary :: Decoded -> String
- onyFinite :: Coefficient -> Exponent -> String
- data Digit
- digitToInt :: Integral a => Digit -> a
- intToDigit :: Integral a => a -> Digit
- digitToChar :: Digit -> Char
- newtype Coefficient = Coefficient {
- unCoefficient :: [Digit]
- coefficient :: [Digit] -> Maybe Coefficient
- newtype Payload = Payload {}
- payload :: [Digit] -> Maybe Payload
- digitsToInteger :: [Digit] -> Integer
- integralToDigits :: Integral a => a -> [Digit]
- coefficientLen :: Int
- payloadLen :: Int
- dIsFinite :: Decoded -> Bool
- dIsInfinite :: Decoded -> Bool
- dIsInteger :: Decoded -> Bool
- dIsLogical :: Decoded -> Bool
- dIsNaN :: Decoded -> Bool
- dIsNegative :: Decoded -> Bool
- dIsNormal :: Decoded -> Bool
- dIsPositive :: Decoded -> Bool
- dIsSignaling :: Decoded -> Bool
- dIsSigned :: Decoded -> Bool
- dIsSubnormal :: Decoded -> Bool
- dIsZero :: Decoded -> Bool
- dDigits :: Coefficient -> Int
- data AdjustedExp = AdjustedExp {
- unAdjustedExp :: Int
- adjustedExp :: Coefficient -> Exponent -> AdjustedExp
- adjustedToExponent :: Coefficient -> AdjustedExp -> Exponent
- dIsSNaN :: Decoded -> Bool
- dIsQNaN :: Decoded -> Bool
- dIsNegInf :: Decoded -> Bool
- dIsNegNormal :: Decoded -> Bool
- dIsNegSubnormal :: Decoded -> Bool
- dIsNegZero :: Decoded -> Bool
- dIsPosZero :: Decoded -> Bool
- dIsPosSubnormal :: Decoded -> Bool
- dIsPosNormal :: Decoded -> Bool
- dIsPosInf :: Decoded -> Bool
Documentation
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.
Constructors
| Ctx | |
Fields
| |
Decimal number. As indicated in the General Decimal
Arithmetic specification, a Quad might be a finite number
(perhaps the most common type) or it might be infinite or a
not-a-number. decClass will tell you a little more about a
particular Quad.
Constructors
| Quad | |
Fields | |
Constructors
| Round | |
Fields | |
Round toward negative infinity.
Round away from zero, but only if the discarded digits are greater than or equal to half of the value of a one in the next left position.
Round away from zero if discarded digits are greater than half of the value of a one in the next left position. If discarded digits are less than half, ignore the discarded digits. If they represent exactly half, do not alter result coefficient if its rightmost digit is even, or increment it by one if its rightmost digit is odd (to make an even digit).
If the discarded digits represent greater than half of the value of a one in the next left position then the result coefficient is incremented by one (that is, rounded away from zero). Otherwise the discarded digits are ignored.
Round toward negative infinity. If all discarded digits are zero or if the sign is zero, the result is unchanged. Otherwise, the sign is 1 and the result coefficient is incremented by 1.
A single error or warning condition that may be set in the
Ctx.
Constructors
| Flag C'uint32_t |
divisionUndefined :: FlagSource
0/0 is undefined. It sets this flag and returns a quiet NaN.
A non-zero dividend is divided by zero. Unlike 0/0, it has a
defined result (a signed Infinity).
divisionImpossible :: FlagSource
Sometimes raised by divideInteger and remainder.
invalidOperation :: FlagSource
Raised on a variety of invalid operations, such as an attempt
to use compareSignal on an operand that is an NaN.
conversionSyntax :: FlagSource
A source string (for instance, in fromByteString) contained
errors.
The current status flags, which indicate results from previous computations.
runCtx :: Ctx a -> IO (a, Flags)Source
By default, rounding is set to roundHalfEven. No status flags are set
initially. Returns the final status flags along with the result
of the computation.
Different categories of Quad.
Constructors
| DecClass C'decClass |
negSubnormal :: DecClassSource
Negative subnormal number
posSubnormal :: DecClassSource
A positive subnormal number
type Binary = Ptr C'decQuad -> Ptr C'decQuad -> Ptr C'decQuad -> Ptr C'decContext -> IO (Ptr C'decQuad)Source
binaryCtxFree :: BinaryCtxFree -> Quad -> Quad -> IO QuadSource
type Ternary = Ptr C'decQuad -> Ptr C'decQuad -> Ptr C'decQuad -> Ptr C'decQuad -> Ptr C'decContext -> IO (Ptr C'decQuad)Source
type GetRounded a = Ptr C'decQuad -> Ptr C'decContext -> C'rounding -> IO aSource
getRounded :: GetRounded a -> Round -> Quad -> Ctx aSource
Absolute value. NaNs are handled normally (the sign of an NaN
is not affected, and an sNaN sets invalidOperation.
and :: Quad -> Quad -> Ctx QuadSource
Digit-wise logical and. Operands must be:
- zero or positive
- integers
- comprise only zeroes and/or ones
If not, invalidOperation is set.
compareSignal :: Quad -> Quad -> Ctx QuadSource
Same as compare, but a quietNaN is treated like a signaling
NaN (sets invalidOperation).
compareTotal :: Quad -> Quad -> IO QuadSource
Compares using an IEEE 754 total ordering, which takes into
account the exponent. IEEE 754 says that this function might
return different results depending upon whether the operands are
canonical; Quad are always canonical so you don't need to worry
about that here.
compareTotalMag :: Quad -> Quad -> IO QuadSource
Same as compareTotal but compares the absolute value of the
two arguments.
copySign :: Quad -> Quad -> IO QuadSource
copySign x y returns z, which is a copy of x but has the
sign of y. This function never raises any signals.
divideInteger :: Quad -> Quad -> Ctx QuadSource
divideInteger x y returns the integer part of the result
(rounded toward zero), with an exponent of 0. If the the result
would not fit because it has too many digits,
divisionImpossible is set.
fma :: Quad -> Quad -> Quad -> Ctx QuadSource
Fused multiply add; fma x y z calculates x * y + z. The
multiply is carried out first and is exact, so the result has
only one final rounding.
fromByteString :: ByteString -> Ctx QuadSource
Reads a ByteString, which can be in scientific, engineering, or
"regular" decimal notation. Also reads NaN, Infinity, etc.
Will return a signaling NaN and set invalidOperation if the
string given is invalid.
In the decNumber C library, this function was called
fromString; the name was changed here because it doesn't take a
regular Haskell String.
fromUInt32 :: C'uint32_t -> IO QuadSource
invert :: Quad -> Ctx QuadSource
Digit-wise logical inversion. The operand must be:
- zero or positive
- integers
- comprise only zeroes and/or ones
If not, invalidOperation is set.
isInfinite :: Quad -> IO BoolSource
True for infinities.
isInteger :: Quad -> IO BoolSource
True if x is finite and has exponent of 0; False otherwise.
This can lead to unexpected results; for instance, 3 x 10 ^ 2 is
300, but this function will return False.
isLogical :: Quad -> IO BoolSource
True only if x is zero or positive, an integer (finite with
exponent of 0), and the coefficient is only zeroes and/or ones.
isNegative :: Quad -> IO BoolSource
True only if x is less than zero and is not an NaN.
isPositive :: Quad -> IO BoolSource
True only if x is greater than zero and is not an NaN.
isSignaling :: Quad -> IO BoolSource
True only if x is a signaling NaN.
isSigned :: Quad -> IO BoolSource
True only if x has a sign of 1. Note that zeroes and NaNs
may have sign of 1.
isSubnormal :: Quad -> IO BoolSource
True only if x is subnormal - that is, finite, non-zero, and
with a magnitude less than 10 ^ emin.
logB :: Quad -> Ctx QuadSource
logB x Returns the adjusted exponent of x, according to IEEE
754 rules. If x is infinite, returns +Infinity. If x is
zero, the result is -Infinity, and divisionByZero is set. If
x is less than zero, the absolute value of x is used. If x
is one, the result is 0. NaNs are propagated as for arithmetic
operations.
max :: Quad -> Quad -> Ctx QuadSource
max x y returns the larger argument; if either (but not both)
x or y is a quiet NaN then the other argument is the result;
otherwise, NaNs, are handled as for arithmetic operations.
min :: Quad -> Quad -> Ctx QuadSource
min x y returns the smaller argument; if either (but not both)
x or y is a quiet NaN then the other argument is the result;
otherwise, NaNs, are handled as for arithmetic operations.
minus :: Quad -> Ctx QuadSource
Negation. Result has the same effect as 0 - x when the
exponent of the zero is the same as that of x, if x is
finite.
nextToward :: Quad -> Quad -> Ctx QuadSource
nextToward x y returns the next Quad in the direction of
y.
or :: Quad -> Quad -> Ctx QuadSource
Digit wise logical inclusive Or. Operands must be:
- zero or positive
- integers
- comprise only zeroes and/or ones
If not, invalidOperation is set.
plus :: Quad -> Ctx QuadSource
Same effect as 0 + x where the exponent of the zero is the
same as that of x if x is finite). NaNs are handled as for
arithmetic operations.
quantize :: Quad -> Quad -> Ctx QuadSource
quantize x y returns z which is x set to have the same
quantum as y; that is, numerically the same value but rounded
or padded if necessary to have the same exponent as y. Useful
for rounding monetary quantities.
reduce :: Quad -> Ctx QuadSource
Reduces coefficient to its shortest possible form without changing the value of the result by removing all possible trailing zeroes.
remainder :: Quad -> Quad -> Ctx QuadSource
Remainder from integer division. If the intermediate integer
does not fit within a Quad, divisionImpossible is raised.
remainderNear :: Quad -> Quad -> Ctx QuadSource
Like remainder but the nearest integer is used for for the
intermediate result instead of the result from divideInteger.
rotate :: Quad -> Quad -> Ctx QuadSource
rotate x y rotates the digits of x to the left (if y is
positive) or right (if y is negative) without adjusting the
exponent or sign of x. y is the number of positions to
rotate and must be in the range negate to
coefficientLen.
coefficentLen
NaNs are propagated as usual. No status is set unless y is
invalid or an operand is an NaN.
sameQuantum :: Quad -> Quad -> IO BoolSource
True only if both operands have the same exponent or are both NaNs (quiet or signaling) or both infinite.
scaleB :: Quad -> Quad -> Ctx QuadSource
scaleB x y calculates x * 10 ^ y. y must be an integer
(finite with exponent of 0) in the range of plus or minus 2 *
, typically resulting from
coefficientLen + coefficientLen)logB. Underflow and overflow might occur; NaNs propagate as
usual.
shift :: Quad -> Quad -> Ctx QuadSource
shift x y shifts digits to the left (if y is positive) or
right (if y is negative) without adjusting the exponent or
sign of y. Any digits shiften in from the left or right will
be 0.
y is a count of positions to shift; it must be a finite
integer in the range negate to
coefficientLencoefficientLen. NaNs propagate as usual. If x is infinite
the result is an infinity of the same sign. No status is set
unless y is invalid or the operand is an NaN.
toByteString :: Quad -> IO ByteStringSource
toEngByteString :: Quad -> IO ByteStringSource
Returns a string in engineering notation.
In the decNumber C library, this is called toEngString; the
name is changed here because the function does not return a
regular Haskell String.
toInt32 :: Round -> Quad -> Ctx C'int32_tSource
Uses the rounding method given rather than the one in the
Ctx. If the operand is infinite, an NaN, or if the result of
rounding is outside the range of a C'int32_t, then
invalidOperation is set. inexact is not set even if rounding
occurred.
toIntegralExact :: Quad -> Ctx QuadSource
toUInt32 :: Round -> Quad -> Ctx C'uint32_tSource
toUInt32 r x returns the value of x, rounded to an integer
if necessary using the rounding mode r rather than the one
given in the Ctx. If x is infinite, or outside of the range
of a C'uint32_t, then invalidOperation is set. inexact is
not set even if rounding occurs.
The negative zero converts to 0 and is valid, but negative numbers are not valid.
toUInt32Exact :: Round -> Quad -> Ctx C'uint32_tSource
version :: IO ByteStringSource
Identifies the version of the decNumber C library.
xor :: Quad -> Quad -> Ctx QuadSource
Digit-wise logical exclusive or. Operands must be:
- zero or positive
- integers
- comprise only zeroes and/or ones
If not, invalidOperation is set.
minNormalAdj :: AdjustedExpSource
The smallest possible adjusted exponent that is still normal. Adjusted exponents smaller than this are subnormal.
minNormalExp :: Coefficient -> ExponentSource
Like minNormalAdj, but returns the size of the regular exponent
rather than the adjusted exponent.
The signed integer which indicates the power of ten by which the coefficient is multiplied.
Constructors
| Exponent | |
Fields
| |
exponent :: Int -> Maybe ExponentSource
Ensures that the exponent is within the range allowed by
minMaxExp.
zeroExponent :: ExponentSource
An Exponent whose value is 0.
A pure Haskell type which holds information identical to that
in a Quad.
toBCD :: Quad -> IO DecodedSource
Decodes a Quad to a pure Haskell type which holds identical
information.
scientific :: Decoded -> StringSource
Converts a Decoded to scientific notation. Unlike
toByteString this will always use scientific notation. For
NaNs and infinities, the notation is identical to that of
decNumber (see Decimal Arithmetic Specification page 19). This
means that a quiet NaN is NaN while a signaling NaN is sNaN,
and infinity is Infinity.
Like decQuadToString, the payload of an NaN is not shown if it is zero.
sciFinite :: Coefficient -> Exponent -> StringSource
ordinary :: Decoded -> StringSource
Converts Decoded to ordinary decimal notation. For NaNs and
infinities, the notation is identical to that of scientific.
Unlike scientific, though the result can always be converted back
to a Quad using fromByteString, the number of significant
digits might change. For example, though 1.2E3 has two
significant digits, using ordinary on this value and then
reading it back in with fromByteString will give you 1200E0,
which has four significant digits.
onyFinite :: Coefficient -> Exponent -> StringSource
A single decimal digit.
digitToInt :: Integral a => Digit -> aSource
intToDigit :: Integral a => a -> DigitSource
digitToChar :: Digit -> CharSource
newtype Coefficient Source
A list of digits, less than or equal to coefficientLen long.
Corresponds only to finite numbers.
Constructors
| Coefficient | |
Fields
| |
Instances
coefficient :: [Digit] -> Maybe CoefficientSource
Creates a Coefficient. Checks to ensure it is not null and
that it is not longer than coefficientLen and that it does not
have leading zeroes (if it is 0, a single D0 is allowed).
A list of digits, less than or equal to payloadLen
long. Accompanies an NaN, potentially with diagnostic
information (I do not know if decNumber actually makes use of
this.)
payload :: [Digit] -> Maybe PayloadSource
Creates a Payload. Checks to ensure it is not null, not
longer than payloadLen and that it does not have leading zeroes
(if it is 0, a single D0 is allowed).
digitsToInteger :: [Digit] -> IntegerSource
The most significant digit is at the head of the list.
integralToDigits :: Integral a => a -> [Digit]Source
The most significant digit is at the head of the list. Sign of number is not relevant.
Maximum number of digits in a coefficient.
Maximum number of digits in a payload.
dIsInfinite :: Decoded -> BoolSource
dIsInteger :: Decoded -> BoolSource
dIsLogical :: Decoded -> BoolSource
True only if x is zero or positive, an integer (finite with
exponent of 0), and the coefficient is only zeroes and/or ones.
The sign must be Sign0 (that is, you cannot have a negative
zero.)
dIsNegative :: Decoded -> BoolSource
True only if x is less than zero and is not an NaN. It's not
enough for the sign to be Sign1; the coefficient (if finite) must
be greater than zero.
dIsPositive :: Decoded -> BoolSource
dIsSignaling :: Decoded -> BoolSource
dIsSubnormal :: Decoded -> BoolSource
dDigits :: Coefficient -> IntSource
The number of significant digits. Zero returns 1.
data AdjustedExp Source
An adjusted exponent is the value of an exponent of a number when that number is expressed as though in scientific notation with one digit before any decimal point. This is the finite exponent + (number of significant digits - 1).
Constructors
| AdjustedExp | |
Fields
| |
Instances
adjustedExp :: Coefficient -> Exponent -> AdjustedExpSource
dIsNegNormal :: Decoded -> BoolSource
dIsNegSubnormal :: Decoded -> BoolSource
dIsNegZero :: Decoded -> BoolSource
dIsPosZero :: Decoded -> BoolSource
dIsPosSubnormal :: Decoded -> BoolSource
dIsPosNormal :: Decoded -> BoolSource