decimal-arithmetic-0.1.0.0: 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 that you 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 P19

This is a number type with 19 decimal digits of precision that rounds half even.

 type Decimal = GeneralDecimal

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

It is also possible to use a decimal number type 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

Instances

(FinitePrecision p, Rounding r) => Enum (Number p r) Source # 

Methods

succ :: Number p r -> Number p r #

pred :: Number p r -> Number p r #

toEnum :: Int -> Number p r #

fromEnum :: Number p r -> Int #

enumFrom :: Number p r -> [Number p r] #

enumFromThen :: Number p r -> Number p r -> [Number p r] #

enumFromTo :: Number p r -> Number p r -> [Number p r] #

enumFromThenTo :: Number p r -> Number p r -> Number p r -> [Number p r] #

(Precision p, Rounding r) => Eq (Number p r) Source # 

Methods

(==) :: Number p r -> Number p r -> Bool #

(/=) :: Number p r -> Number p r -> Bool #

(FinitePrecision p, Rounding r) => Fractional (Number p r) Source # 

Methods

(/) :: Number p r -> Number p r -> Number p r #

recip :: Number p r -> Number p r #

fromRational :: Rational -> Number p r #

(Precision p, Rounding r) => Num (Number p r) Source # 

Methods

(+) :: Number p r -> Number p r -> Number p r #

(-) :: Number p r -> Number p r -> Number p r #

(*) :: Number p r -> Number p r -> Number p r #

negate :: Number p r -> Number p r #

abs :: Number p r -> Number p r #

signum :: Number p r -> Number p r #

fromInteger :: Integer -> Number p r #

(Precision p, Rounding r) => Ord (Number p r) Source # 

Methods

compare :: Number p r -> Number p r -> Ordering #

(<) :: Number p r -> Number p r -> Bool #

(<=) :: Number p r -> Number p r -> Bool #

(>) :: Number p r -> Number p r -> Bool #

(>=) :: Number p r -> Number p r -> Bool #

max :: Number p r -> Number p r -> Number p r #

min :: Number p r -> Number p r -> Number p r #

(Precision p, Rounding r) => Read (Number p r) Source # 
(Precision p, Rounding r) => Real (Number p r) Source # 

Methods

toRational :: Number p r -> Rational #

(FinitePrecision p, Rounding r) => RealFrac (Number p r) Source # 

Methods

properFraction :: Integral b => Number p r -> (b, Number p r) #

truncate :: Integral b => Number p r -> b #

round :: Integral b => Number p r -> b #

ceiling :: Integral b => Number p r -> b #

floor :: Integral b => Number p r -> b #

Show (Number p r) Source # 

Methods

showsPrec :: Int -> Number p r -> ShowS #

show :: Number p r -> String #

showList :: [Number p r] -> ShowS #

Precision p => Precision (Number p r) Source # 

Methods

precision :: Number p r -> Maybe Int Source #

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.

Minimal complete definition

precision

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

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