decimal-arithmetic-0.1.0.1: An implementation of Mike Cowlishaw's General Decimal Arithmetic Specification

Copyright© 2016 Robert Leslie
LicenseBSD3
Maintainerrob@mars.org
Stabilityexperimental
Safe HaskellTrustworthy
LanguageHaskell2010

Numeric.Decimal

Contents

Description

This module provides a general-purpose Number type supporting decimal arithmetic for both limited precision floating-point (IEEE 754-2008) and for arbitrary precision floating-point (following the same principles as IEEE 754 and IEEE 854-1987) as described in the General Decimal Arithmetic Specification by Mike Cowlishaw. In addition to floating-point arithmetic, integer and unrounded floating-point arithmetic are included as subsets.

Unlike the binary floating-point types Float and Double, the Number type can represent and perform arithmetic with decimal numbers exactly. Internally, a Number is represented with an integral coefficient and base-10 exponent.

The Number type supports lossless conversion to and from a string representation via the Show and Read instances. Note that there may be multiple representations of values that are numerically equal (e.g. 1 and 1.00) which are preserved by this conversion.

Synopsis

Usage

It is recommended to create an alias for the type of numbers you wish to support in your application. For example:

 type Decimal = BasicDecimal

This is a basic number type with 9 decimal digits of precision that rounds half up.

 type Decimal = ExtendedDecimal P15

This is a number type with 15 decimal digits of precision that rounds half even. There are a range of ready-made precisions available, including P1 through P50 on up to P2000. Alternatively, an arbitrary precision can be constructed through type application of PPlus1 and/or PTimes2 to any existing precision.

 type Decimal = GeneralDecimal

This is a number type with infinite precision. Note that not all operations support numbers with infinite precision.

 type Decimal = Number P34 RoundDown

This is a custom number type with 34 decimal digits of precision that rounds down (truncates). Several Rounding algorithms are available to choose from.

A decimal number type may be used in a default declaration, possibly replacing Double or Integer. For example:

 default (Integer, Decimal)

Arbitrary-precision decimal numbers

data Number p r Source

A decimal floating point number with selectable precision and rounding algorithm

type BasicDecimal = Number P9 RoundHalfUp Source

A basic decimal floating point number with 9 digits of precision, rounding half up

type ExtendedDecimal p = Number p RoundHalfEven Source

A decimal floating point number with selectable precision, rounding half even

type GeneralDecimal = ExtendedDecimal PInfinite Source

A decimal floating point number with infinite precision

Precision types

class Precision p where Source

Precision indicates the maximum number of significant digits a number may have.

Methods

precision :: p -> Maybe Int Source

Return the precision of the argument, or Nothing if the precision is infinite.

class Precision p => FinitePrecision p Source

A subclass of precisions which are finite

data P1 Source

A precision of 1 significant digit

type P2 = PTimes2 P1 Source

A precision of 2 significant digits

type P3 = PPlus1 P2 Source

A precision of 3 significant digits

type P4 = PTimes2 P2 Source

Et cetera

type P75 = PPlus1 P74 Source

type P250 = PTimes2 P125 Source

data PPlus1 p Source

A precision of (p + 1) significant digits

data PTimes2 p Source

A precision of (p × 2) significant digits

data PInfinite Source

A precision of unlimited significant digits

Rounding types

class Rounding r Source

A rounding algorithm to use when the result of an arithmetic operation exceeds the precision of the result type

Minimal complete definition

round

data RoundHalfUp Source

If the discarded digits represent greater than or equal to half (0.5) of the value of a one in the next left position then the value is rounded up. If they represent less than half, the value is rounded down.

data RoundHalfEven Source

If the discarded digits represent greater than half (0.5) of the value of a one in the next left position then the value is rounded up. If they represent less than half, the value is rounded down. If they represent exactly half, the value is rounded to make its rightmost digit even.

data RoundHalfDown Source

If the discarded digits represent greater than half (0.5) of the value of a one in the next left position then the value is rounded up. If they represent less than half or exactly half, the value is rounded down.

data RoundCeiling Source

Round toward +∞

data RoundFloor Source

Round toward −∞

data RoundUp Source

Round away from 0

data Round05Up Source

Round zero or five away from 0

data RoundDown Source

Round toward 0 (truncate)

Functions

cast :: (Precision p, Rounding r) => Number a b -> Number p r Source

Cast a Number to another precision and/or rounding algorithm, immediately rounding if necessary to the new precision using the new algorithm.