deka-0.6.0.0: Decimal floating point arithmetic

Safe HaskellSafe-Inferred
LanguageHaskell2010

Deka.Native

Contents

Description

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:

  • infinity - numbers infinitely large in magnitude
  • quiet NaN - an undefined result which does not cause an invalidOperation condition.
  • signaling NaN - an undefined result which will usually cause an invalidOperation condition.

When a number has one of these special values, its coefficient and exponent are undefined. An NaN, however, may have additional diagnostic information, which is a positive integer.

All special values have a sign. The sign of an infinity is significant. The sign of an NaN has no meaning, though it may be considered as part of the diagnostic information.

You can transform an abstract form to a Dec losslessly by using abstractToByteString. This gives you a string in scientific notation, as specified in to-scientific-string in the specification. There is a one-to-one mapping of abstract representations to scientific-string representations. You can also transform a Dec to an Abstract losslessly by using abstractFromByteString. This operation will not fail if it is using output from toByteString; but it might fail otherwise, if the input is malformed.

All standard typeclass instances in these modules are derived; so while the Ord instance might be useful to use Abstract as the key in a Map, don't expect it to tell you anything about how Abstract are situated on the number line.

Synopsis

Digits and groups of digits

data Novem Source

A digit from one to nine. Useful to represent a most significant digit, or MSD, as an MSD cannot be the digit zero.

Constructors

D1 
D2 
D3 
D4 
D5 
D6 
D7 
D8 
D9 

data Decem Source

A digit from zero to nine.

Constructors

D0 
Nonem Novem 

Instances

data Decuple Source

A non-empty set of digits. The MSD must be from 1 to 9.

Constructors

Decuple Novem [Decem] 

data Aut Source

Either a set of digits, or zero. Unsigned.

Constructors

Nil

Zero

Plenus Decuple

Non-zero

Instances

data Firmado Source

Either a set of digits, or zero. Signed.

Constructors

Cero

Zero

Completo PosNeg Decuple

Non-zero

Elements of abstract numbers

newtype Coefficient Source

The coefficient in a number; not used in infinities or NaNs.

Constructors

Coefficient 

Fields

unCoefficient :: Aut
 

newtype Exponent Source

The exponent in a number.

Constructors

Exponent 

Fields

unExponent :: Firmado
 

newtype Diagnostic Source

The diagnostic information in an NaN.

Constructors

Diagnostic 

data Noisy Source

Whether an NaN is quiet or signaling.

Constructors

Quiet 
Signaling 

Instances

data NonNum Source

Not a Number.

Constructors

NonNum 

Instances

data Value Source

All data in an abstract number except for the sign.

Instances

data Abstract Source

Abstract representation of all numbers covered by the General Decimal Arithmetic Specification.

Constructors

Abstract 

Fields

sign :: Sign
 
value :: Value
 

Transformations

abstractToString :: Abstract -> String Source

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.

abstractToDec :: Abstract -> (Dec, Flags) Source

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.

stringToAbstract Source

Arguments

:: String

Input string

-> Either String Abstract

Returns a Right with the abstract representation of the input string, if the input conformed to the numeric string specification given in the General Decimal Arithmetic Specification. Otherwise, returns a Left with an error message.

decToAbstract :: Dec -> Abstract Source

Transforms a Dec to an Abstract.