Bucklescript bindings to big.js, a library for arbitrary-precision decimal arithmetic. All methods are supported.
- Make sure big.js is installed:
npm install big.js
- Install the bindings:
npm install bs-big.js
- Add the bindings to bs-config.json:
{
"bs-dependencies": [
"bs-big.js"
]
}
Big numbers of type Big.t
can be created using Big.fromInt
, Big.fromString
or Big.fromFloat
. Arithmetic can
then be done using the pipe-first |.
syntax. For example:
Big.setDP(10); //set decimal places
Big.setRM(1); //set rounding mode
let x = Big.fromInt(2);
let y = Big.fromInt(3);
let z = x |. Big.div(y);
let _ = z |. Big.toString // "0.6666666667"
let _ = z |. Big.sqrt |. Big.toString // "0.8164965809"
let _ = z |. Big.pow(-3) |. Big.toString // "3.3749999995"
let _ = z |. Big.times(z) |. Big.toString // "0.44444444448888888889"
let _ = z |. Big.times(z) |. Big.round(~dp=10, ()) |. Big.toString // "0.4444444445"
See Big.mli and the big.js documentation
Very extensive testing was done by porting most of the tests from big.js. To run the tests:
npm test
- The method
Big.mod_
has an underscore becausemod
is a reserved keyword in OCaml/Reason - The last parameter of all methods with optional parameters is
unit
. This is because the last parameter would be optional, which don't work properly in OCaml. Keeping the parameter order allows consistent use of the pipe-first syntax|.
, so it is better to end withunit
than swap order. - OCaml does not have
-0
but Javascript does