Bucklescript bindings to big.js, a library for arbitrary-precision decimal arithmetic. All methods are supported.
npm install big.js
npm install bs-big.js
{
"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
Big.mod_
has an underscore because mod
is a reserved keyword in OCaml/Reasonunit
. 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 with unit
than swap order.-0
but Javascript does