BuckleScript bindings for bn.js.
This is a work in progress, so proceed with caution. However, 90% of the tests in the original library have been migrated to Reason, so the coverage is mostly complete.
Constructors, binary ops, arithmetic ops, and reduction ops are available. There are some functions related to prime numbers that have not been bound yet. Feel free to open an issue / PR if you need them.
bn.js
installed. If not, run npm install bn.js --save
npm install bs-bn.js --save
bsconfig.json
{
"bs-dependencies": [
"bs-bn.js"
]
}
Create new big numbers from float numbers:
let a = Bn.fromFloat(12345.);
let a = Bn.fromFloat 12345.
You can use float_of_int
if you prefer to use ints. The main reason why floats were selected is that ints are trimmed to 32 bits in BuckleScript, so float is a safer choice.
You can create big numbers also from other types:
let s = Bn.fromString(~base=16, ~endian=`le, "1A6B765D8CDF");
let a = Bn.fromArray([|1, 2, 3|]);
let b = Bn.fromBuffer(Node.Buffer.fromString("12345"));
let s = Bn.fromString ~base:16 ~endian:`le "1A6B765D8CDF"
let a = Bn.fromArray [|1;2;3|]
let b = Bn.fromBuffer (Node.Buffer.fromString "12345")
Operations always keep the affected big number as the last param to enable piping:
let a = Bn.fromString(~base=16, "ffffffff");
let b = a |> Bn.abs |> Bn.mul(Bn.fromFloat(3.));
let a = Bn.fromString ~base:16 "ffffffff"
let b = (a |> Bn.abs) |> (Bn.mul (Bn.fromFloat 3.))
Check the interface file Bn.rei
.
let result = Bn.Infix.(bn1 + bn2);