deka-0.6.0.2: Decimal floating point arithmetic

Safe HaskellSafe
LanguageHaskell2010

Deka.Context

Contents

Synopsis

Integer type

type Signed = Int64 Source

A signed integer. Its size is platform dependent.

Ctx

data Ctx a Source

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

data Flag Source

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 Flags Source

A container of Flag.

allFlag :: [Flag] Source

A list of all possible Flag, in order.

fullFlags :: Flags Source

All possible Flag are set.

emptyFlags :: Flags Source

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.

divisionByZero :: Flag Source

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.

inexact :: Flag Source

One or more non-zero coefficient digits were discarded during rounding.

invalidContext :: Flag Source

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.

overflow :: Flag Source

The exponent of a result is too large to be represented.

underflow :: Flag Source

A result is both subnormal and inexact.

Traps

getTraps :: Ctx Flags Source

Gets all currently set 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

getStatus :: Ctx Flags Source

All currently set status flags.

setStatus :: Flags -> Ctx () Source

Sets status flags. All existing status flags are cleared and replaced with the ones you indicate here.

Digits

data Precision Source

Sets the precision to be used for all operations. The result of an operation is rounded to this length if necessary.

precision :: Signed -> Maybe Precision Source

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.

setMaxPrecision :: Ctx Precision Source

Sets the precision to the maximum possible, respecting that Emax > 5 * Precision. Returns the new Precision.

Rounding

Rounding types

data Round Source

Instances

roundCeiling :: Round Source

Round toward positive infinity.

roundUp :: Round Source

Round away from zero.

roundHalfUp :: Round Source

0.5 rounds up

roundHalfEven :: Round Source

0.5 rounds to nearest even

roundHalfDown :: Round Source

0.5 rounds down

roundDown :: Round Source

Round toward zero - truncate

roundFloor :: Round Source

Round toward negative infinity.

round05Up :: Round Source

Round for reround

roundTruncate :: Round Source

Truncate, but set infinities.

Getting and setting

Emax and Emin

Emax

data Emax Source

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.

emax :: Signed -> Maybe Emax Source

Returns an Emax for use in setTrio. Fails if argument is out of range.

Emin

data Emin Source

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.

emin :: Signed -> Maybe Emin Source

Returns an Emin for use in setTrio. Fails if argument is out of range.

Trio

data Trio Source

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.

Instances

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.

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:

  • Precision of ((1 * 10 ^ 18) - 1)
  • Emax of ((1 * 10 ^ 18) - 1)
  • Emin of -((1 * 10 ^ 18) - 1)

On 32-bit platforms, the maximums are:

Constructors

Max

Sets:

As noted in the documentation for Trio, the specification requires that Emax > 5 * Precision; Max does not respect this.

Default

Same as Max, except:

  • Precision is 2 * MPD_RDIGITS
Basic

Same as Max, except:

Pedantic

Sets the maximum allowable figures, while respecting the restriction that stated in the specification and the mpdecimal documentation, which is that Emax > 5 * Precision. Also, sets no traps. This sets:

Decimal32

Sets:

Decimal64

Same as Decimal32, except:

Decimal128

Same as Decimal32, except:

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.

runCtx :: Ctx a -> a Source

Runs a Ctx computation using the Pedantic Initializer.

runCtxStatus :: Ctx a -> (a, Flags) Source

Like runCtx but also returns any status flags resulting from the computation.

local Source

Arguments

:: 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.