HasBigDecimal: A library for arbitrary precision decimal numbers.

[ apache, library, math ] [ Propose Tags ]

A native Haskell implementation of arbitrary precicion decimal numbers, based on Haskell Integers. Inspired by Java BigDecimals

[Skip to Readme]
Versions [RSS] [faq] 0.1.1
Dependencies base (>=4.7 && <5) [details]
License Apache-2.0
Copyright 2018 Thomas Mahler
Author Thomas Mahler
Maintainer thma@apache.org
Category Math
Home page https://github.com/thma/HasBigDecimal#readme
Source repo head: git clone https://github.com/thma/HasBigDecimal
Uploaded by thma at 2018-05-20T20:41:14Z
Distributions LTSHaskell:0.1.1, NixOS:0.1.1, Stackage:0.1.1
Downloads 1083 total (32 in the last 30 days)
Rating 2.0 (votes: 1) [estimated by Bayesian average]
Your Rating
  • λ
  • λ
  • λ
Status Hackage Matrix CI
Docs available [build log]
Last success reported on 2018-05-20 [all 1 reports]




Maintainer's Corner

For package maintainers and hackage trustees


Readme for HasBigDecimal-0.1.1

[back to package description]

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