A library for arbitrary precision decimal numbers.


Version on this page:0.1.1
LTS Haskell 22.26:
Stackage Nightly 2024-06-22:
Latest on Hackage:

See all snapshots HasBigDecimal appears in

Apache-2.0 licensed by Thomas Mahler
Maintained by [email protected]
This version can be pinned in stack with:HasBigDecimal-0.1.1@sha256:5e20dec5990b9567ad8ef8e767d566e4abf6450bb5cc9ca6a4a1ec27a42aaaaf,1331

Module documentation for 0.1.1

Depends on 1 package(full list with versions):
Used by 1 package in lts-16.31(full list with versions):

Build Status


This module defines the type ‘BigDecimal’ which provides a representation of arbitrary precision decimal numbers. ‘BigDecimal’ is a native Haskell implementation based on arbitrary sized ‘Integer’ values. The implementation was inspired by Java BigDecimals.

BigDecimal instantiates the typeclasses ‘Num’, ‘Fractional’ and ‘Real’. It is thus possible to use all common operators like ‘+’, ‘-’, ‘*’, ‘/’, ‘^’ on them.

Some examples from a ghci REPL

λ> a = BigDecimal 144 2
λ> toString a
λ> b = sqrt a
λ> toString b
λ> b * b
BigDecimal 144 2
λ> b * b * b
BigDecimal 1728 3
λ> b^2
BigDecimal 144 2
λ> c = fromString "123.4567890"
λ> c
BigDecimal 1234567890 7
λ> a / c
BigDecimal 1166400010614240096589584878965222398584 41
λ> roundBD it (halfUp 10)
BigDecimal 116640001 10
λ> divide (a, c) $ halfUp 20
BigDecimal 1166400010614240097 20


in addition to the pretty complete BigDecimal module there is the rather scetchy BigFloating module. BigFloating contains a few first step to let BigDecimal instantiate the Floating typeclass. As of now it contains arbitrary precision implementations for pi (based on Chudnovskis algorithm), sqrt and nthroot (based on Newtons classic algorithm). All trigonometric functions, log and exp are still missing. All code contributions are most welcome! Here are some working examples:

λ> r = sqrt (BigDecimal 2 0)
λ> toString r
λ> r^2*pi
BigDecimal 6283185307179586476925286766559005768394338798750211641949889184615632812572417997256069650684234135488875159962758271904785109490094314219117662951460673928547017151357805018682925970564827587058974690236729643325013696514697383143361638452329945607739055327681644609147889519349178329780951524191191 300
λ> toString it

λ>  sqr 2 (halfUp 50)
BigDecimal 141421356237309504880168872420969807856967187537695 50
λ>  sqr 2 (halfUp 500)
BigDecimal 141421356237309504880168872420969807856967187537694807317667973799073247846210703885038753432764157273501384623091229702492483605585073721264412149709993583141322266592750559275579995050115278206057147010955997160597027453459686201472851741864088919860955232923048430871432145083976260362799525140798968725339654633180882964062061525835239505474575028775996172983557522033753185701135437460340849884716038689997069900481503054402779031645424782306849293691862158057846311159666871301301561856898723724 500