Is there a Bignum module for Ocaml?

There is no bignum module in the core OCaml distribution. The official recommendation is to use the third-party Zarith library, specifically its Z module.

If you've installed OCaml as part of your operating system distribution, see if it has a package for Zarith. For example, on Debian, Ubuntu and derivatives, it's libzarith-ocaml-dev. If Zarith isn't available through your operating system, install it with OPAM:

opam install zarith

When building your application, compile with one of

ocamlc -I +zarith zarith.cma …
ocamlopt -I +zarith zarith.cmxa …
ocamlfind <COMMAND> -package zarith -linkpkg …

To use Zarith in the toplevel:

ocaml -I +zarith zarith.cma zarith_top.cma

Older versions of Ocaml had a bignum module in the standard distribution (but not in the standard library: it didn't require separate installation, but had to be linked explicitly). It was the nums library, and its main module Num. Since OCaml 4.06, the nums library is distributed separately.

Technically, there is no bignum module in the standard library, because the standard library in Ocaml terminology is the default library that programs are compiled against, and there's no bignum module in that. Nor is there a Bignum library — because the bignum library is called nums, and its main module is called Num. It has been part of the standard Ocaml distribution since before Ocaml was called Ocaml, and still is.


You are looking for the num library. Technically this is not part of the "Standard Library", but is part of the standard distribution. Thus, simply compile with this library, and it will be available. Also, you might be interested in the new zarith library. I am unsure how the two compare.


To add to the other answers, the general arbitrary precision module is Num (it includes big integers and big rational numbers). There is separately a Big_int module for just big integers; the Big_int module is used by Num for its big integer support, but if you need just big integers and not fractions, you can just use Big_int directly. Both are part of the num library.

Tags:

Ocaml