GithubHelp home page GithubHelp logo

gusbicalho / hasbigdecimal Goto Github PK

View Code? Open in Web Editor NEW

This project forked from thma/hasbigdecimal

0.0 1.0 0.0 119 KB

simple implementation of big decimals in Haskell - inspired by Java BigDecimals

License: Apache License 2.0

Haskell 100.00%

hasbigdecimal's Introduction

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

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
"1.44"
λ> b = sqrt a
λ> toString b
"1.2"
λ> 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

BigFloating

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
"1.4142135623730950488016887242096980785696718753769480731766797379907324784621070388503875343276415727"
λ> r^2*pi
BigDecimal 6283185307179586476925286766559005768394338798750211641949889184615632812572417997256069650684234135488875159962758271904785109490094314219117662951460673928547017151357805018682925970564827587058974690236729643325013696514697383143361638452329945607739055327681644609147889519349178329780951524191191 300
λ> toString it
"6.283185307179586476925286766559005768394338798750211641949889184615632812572417997256069650684234135488875159962758271904785109490094314219117662951460673928547017151357805018682925970564827587058974690236729643325013696514697383143361638452329945607739055327681644609147889519349178329780951524191191"

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

hasbigdecimal's People

Contributors

thma avatar gusbicalho avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.