Safe Haskell | Safe |
---|---|
Language | Haskell2010 |
- type Signed = Int64
- data Ctx a
- data Flag
- data Flags
- allFlag :: [Flag]
- fullFlags :: Flags
- emptyFlags :: Flags
- packFlags :: [Flag] -> Flags
- unpackFlags :: Flags -> [Flag]
- clamped :: Flag
- conversionSyntax :: Flag
- divisionByZero :: Flag
- divisionImpossible :: Flag
- divisionUndefined :: Flag
- fpuError :: Flag
- inexact :: Flag
- invalidContext :: Flag
- invalidOperation :: Flag
- mallocError :: Flag
- notImplemented :: Flag
- overflow :: Flag
- rounded :: Flag
- subnormal :: Flag
- underflow :: Flag
- getTraps :: Ctx Flags
- setTraps :: Flags -> Ctx ()
- getStatus :: Ctx Flags
- setStatus :: Flags -> Ctx ()
- data Precision
- precision :: Signed -> Maybe Precision
- unPrecision :: Precision -> Signed
- getPrecision :: Ctx Precision
- setMaxPrecision :: Ctx Precision
- data Round
- roundCeiling :: Round
- roundUp :: Round
- roundHalfUp :: Round
- roundHalfEven :: Round
- roundHalfDown :: Round
- roundDown :: Round
- roundFloor :: Round
- round05Up :: Round
- roundTruncate :: Round
- getRound :: Ctx Round
- setRound :: Round -> Ctx ()
- data Emax
- unEmax :: Emax -> Signed
- emax :: Signed -> Maybe Emax
- getEmax :: Ctx Emax
- data Emin
- unEmin :: Emin -> Signed
- emin :: Signed -> Maybe Emin
- getEmin :: Ctx Emin
- data Trio
- trioPrecision :: Trio -> Precision
- trioEmax :: Trio -> Emax
- trioEmin :: Trio -> Emin
- trio :: Precision -> Emax -> Emin -> Maybe Trio
- setTrio :: Trio -> Ctx ()
- getTrio :: Ctx Trio
- getClamp :: Ctx Bool
- setClamp :: Bool -> Ctx ()
- getAllCorrectRound :: Ctx Bool
- setAllCorrectRound :: Bool -> Ctx ()
- data Initializer
- initCtx :: Initializer -> Ctx ()
- runCtxInit :: Initializer -> Ctx a -> a
- runCtx :: Ctx a -> a
- runCtxStatus :: Ctx a -> (a, Flags)
- local :: Ctx a -> Ctx a
Integer type
Ctx
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.
Flags
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.
No Flag
are set.
unpackFlags :: Flags -> [Flag] Source
Flags will always be unpacked in order.
Individual flags
conversionSyntax :: Flag Source
A source string (for instance, in fromByteString
) contained
errors.
A non-zero dividend is divided by zero. Unlike 0/0
, it has a
defined result (a signed Infinity).
divisionImpossible :: Flag Source
Sometimes raised by divideInteger
and remainder
.
divisionUndefined :: Flag Source
0/0
is undefined. It sets this flag and returns a quiet NaN.
The Context for computations was invalid; this error should
never occur because deka
keeps you from setting an invalid
context.
invalidOperation :: Flag Source
Raised on a variety of invalid operations, such as an attempt
to use compareSignal
on an operand that is an NaN.
Traps
setTraps :: Flags -> Ctx () Source
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.
Status
setStatus :: Flags -> Ctx () Source
Sets status flags. All existing status flags are cleared and replaced with the ones you indicate here.
Digits
Sets the precision to be used for all operations. The result of an operation is rounded to this length if necessary.
unPrecision :: Precision -> Signed Source
Rounding
Rounding types
Round toward positive infinity.
0.5
rounds up
0.5
rounds to nearest even
0.5
rounds down
Round toward negative infinity.
Truncate, but set infinities.
Getting and setting
Emax and Emin
Emax
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
.
Emin
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.
Trio
trioPrecision :: Trio -> Precision Source
trio :: Precision -> Emax -> Emin -> Maybe Trio Source
Make a new Trio
. Fails if the values are out of range.
Clamp
setClamp :: Bool -> Ctx () Source
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.
Correct rounding
getAllCorrectRound :: Ctx Bool Source
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.
setAllCorrectRound :: Bool -> Ctx () Source
Initializers
data Initializer Source
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:
On 32-bit platforms, the maximums are:
Max | Sets:
As noted in the documentation for |
Default | Same as
|
Basic | Same as
|
Pedantic | Sets the maximum allowable figures, while respecting the
restriction that stated in the specification and the
|
Decimal32 | Sets:
|
Decimal64 | Same as |
Decimal128 | Same as |
initCtx :: Initializer -> Ctx () Source
Re-initialize a Ctx
using the given Initializer.
Running a Ctx
runCtxInit :: Initializer -> Ctx a -> a Source
Runs a Ctx computation; begins with the given Initializer to set up the context.
runCtxStatus :: Ctx a -> (a, Flags) Source
Like runCtx
but also returns any status flags resulting from
the computation.
:: Ctx a | Run this computation. It is initialized with the current Ctx, but does not affect the current Ctx. |
-> Ctx a | Returns the result of the child computation. |
Runs a Ctx computation within the existing Ctx. The existing Ctx is copied to form a new Ctx; then the child computation is run without affecting the parent Ctx.