MyNixOS website logo
Description

A library for arbitrary precision decimal numbers.

Please see the README on GitHub at https://github.com/thma/HasBigDecimal#readme

Actions Status

HasBigDecimal

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. It aims to provide a simple to use API.

-- | BigDecimal is represented by an unscaled Integer value and a Natural that defines the scale
--   E.g.: (BigDecimal value = 1234, scale = 2) represents the decimal value 12.34.
data BigDecimal = BigDecimal
  { 
    value :: Integer,  -- ^ the unscaled Integer value    
    scale :: Natural   -- ^ the scale (i.e. the number of digits after the decimal point)
  }

BigDecimal instantiates the following typeclasses:

instance Eq BigDecimal
instance Ord BigDecimal

instance Num BigDecimal
instance Fractional BigDecimal
instance Real BigDecimal

instance Read BigDecimal
instance Show BigDecimal

It is thus possible to use all common numerical operations on operators like '+', '-', '*', '/', '^' on them.

Some examples from a ghci REPL

λ> a = BigDecimal 144 2
λ> a
1.44
λ> b = sqrt a
λ> b
1.2
λ> b * b
1.44
λ> b^3
1.728

λ> c = fromString "123.4567890"
λ> c
123.4567890

λ> value c
1234567890
λ> scale c
7

λ> a / c
0.01166400010614240096589584878965222398584
λ> roundBD it (halfUp 10)
0.0116640001
λ> divide (a, c) (halfUp 20)
0.01166400010614240097


BigFloating

in addition to the pretty complete BigDecimal module there is the rather scetchy BigFloating module. BigFloating contains a few first steps 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)
λ> r
1.4142135623730950488016887242096980785696718753769480731766797379907324784621070388503875343276415727
λ> r^2*pi
6.2831853071795864769252867665590057683943387987502116419498891846156328125724179972560696506842341354888751599627582719047851094900943142191176629514606739
28547017151357805018682925970564827587058974690236729643325013696514697383143361638452329945607739055327681644609147889519349178329780951524191191

λ> sqr 2 (halfUp 50)
1.41421356237309504880168872420969807856967187537695
λ> sqr 2 (halfUp 500)
1.4142135623730950488016887242096980785696718753769480731766797379907324784621070388503875343276415727350138462309122970249248360558507372126441214970999358
314132226659275055927557999505011527820605714701095599716059702745345968620147285174186408891986095523292304843087143214508397626036279952514079896872533965
463318088296406206152583523950547457502877599617298355752203375318570113543746034084988471603868999706990048150305440277903164542478230684929369186215805784
6311159666871301301561856898723724

λ> piChudnovsky (halfUp 500)
3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679821480865132823066470938446095505822317253594081284811
174502841027019385211055596446229489549303819644288109756659334461284756482337867831652712019091456485669234603486104543266482133936072602491412737245870066
063155881748815209209628292540917153643678925903600113305305488204665213841469519415116094330572703657595919530921861173819326117931051185480744623799627495
6735188575272489122793818301194913


Metadata

Version

0.2.0.0

License

Executables (2)

  • bin/benchmark
  • bin/HasBigDecimal-Demo

Platforms (75)

    Darwin
    FreeBSD
    Genode
    GHCJS
    Linux
    MMIXware
    NetBSD
    none
    OpenBSD
    Redox
    Solaris
    WASI
    Windows
Show all
  • aarch64-darwin
  • aarch64-genode
  • aarch64-linux
  • aarch64-netbsd
  • aarch64-none
  • aarch64_be-none
  • arm-none
  • armv5tel-linux
  • armv6l-linux
  • armv6l-netbsd
  • armv6l-none
  • armv7a-darwin
  • armv7a-linux
  • armv7a-netbsd
  • armv7l-linux
  • armv7l-netbsd
  • avr-none
  • i686-cygwin
  • i686-darwin
  • i686-freebsd
  • i686-genode
  • i686-linux
  • i686-netbsd
  • i686-none
  • i686-openbsd
  • i686-windows
  • javascript-ghcjs
  • loongarch64-linux
  • m68k-linux
  • m68k-netbsd
  • m68k-none
  • microblaze-linux
  • microblaze-none
  • microblazeel-linux
  • microblazeel-none
  • mips-linux
  • mips-none
  • mips64-linux
  • mips64-none
  • mips64el-linux
  • mipsel-linux
  • mipsel-netbsd
  • mmix-mmixware
  • msp430-none
  • or1k-none
  • powerpc-netbsd
  • powerpc-none
  • powerpc64-linux
  • powerpc64le-linux
  • powerpcle-none
  • riscv32-linux
  • riscv32-netbsd
  • riscv32-none
  • riscv64-linux
  • riscv64-netbsd
  • riscv64-none
  • rx-none
  • s390-linux
  • s390-none
  • s390x-linux
  • s390x-none
  • vc4-none
  • wasm32-wasi
  • wasm64-wasi
  • x86_64-cygwin
  • x86_64-darwin
  • x86_64-freebsd
  • x86_64-genode
  • x86_64-linux
  • x86_64-netbsd
  • x86_64-none
  • x86_64-openbsd
  • x86_64-redox
  • x86_64-solaris
  • x86_64-windows