Safe Haskell | Safe-Inferred |
---|---|
Language | Haskell2010 |
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.
- data Novem
- data Decem
- data Decuple = Decuple Novem [Decem]
- data Aut
- data Firmado
- newtype Coefficient = Coefficient {
- unCoefficient :: Aut
- newtype Exponent = Exponent {}
- newtype Diagnostic = Diagnostic {}
- data Noisy
- data NonNum = NonNum {
- noisy :: Noisy
- diagnostic :: Maybe Diagnostic
- data Value
- data Abstract = Abstract {}
- abstractToString :: Abstract -> String
- abstractToDec :: Abstract -> (Dec, Flags)
- stringToAbstract :: String -> Either String Abstract
- decToAbstract :: Dec -> Abstract
Digits and groups of digits
A digit from one to nine. Useful to represent a most significant digit, or MSD, as an MSD cannot be the digit zero.
A non-empty set of digits. The MSD must be from 1 to 9.
Either a set of digits, or zero. Unsigned.
Either a set of digits, or zero. Signed.
Elements of abstract numbers
newtype Coefficient Source
The coefficient in a number; not used in infinities or NaNs.
The exponent in a number.
newtype Diagnostic Source
The diagnostic information in an NaN.
Not a Number.
NonNum | |
|
All data in an abstract number except for the sign.
Abstract representation of all numbers covered by the General Decimal Arithmetic Specification.
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
decToAbstract :: Dec -> Abstract Source
Transforms a Dec
to an Abstract
.