Skip to content
The Zarith library implements arithmetic and logical operations over arbitrary-precision integers and rational numbers. The implementation, based on GMP, is very efficient.
Branch: master
Clone or download
antoinemine Merge pull request #44 from avsm/sync-perl-and-ocaml
update the ocaml configuration script with the perl one
Latest commit b0fbd3b Apr 2, 2019
Type Name Latest commit message Commit time
Failed to load latest commit information.
.gitignore Let Git ignore more compilation residues Apr 10, 2018
LICENSE Added license May 23, 2011
META Update for a 1.8 release Mar 29, 2019 Added ediv, erem, ediv_rem (Euclidean division) Jun 18, 2011
big_int_Z.mli fix ocamlforge bugs 1023 & 1024: Aug 4, 2011
caml_z.c overflow detection when constructing large mpz_t numbers, raise an OC… Apr 2, 2019
caml_z_arm.S fix build on Raspberry Debian Jessie Oct 11, 2017
caml_z_i686.S Non-executable stack continued Sep 23, 2017
caml_z_x86_64.S Non-executable stack continued Sep 23, 2017
caml_z_x86_64_mingw64.S caml_z_x86_64_mingw64: Remove non-executable stack directive (#22) Dec 28, 2017
config.guess - preliminary configure (only supports Linux for now) May 27, 2011
configure Restores backwards compatibility. Nov 7, 2018
project.mak Add package with REPL printer Apr 10, 2018
q.mli Merge pull request #20 from murmour/infix-comparisons Apr 17, 2018
z.mlip Merge pull request #20 from murmour/infix-comparisons Apr 17, 2018
z.mlp Add infix comparison operators to Q and Z Nov 28, 2017 Experimental conversions between zarith and mlgmpidl. Aug 17, 2011
z_mlgmpidl.mli Experimental conversions between zarith and mlgmpidl. Aug 17, 2011 update the ocaml configuration script with the perl one Apr 2, 2019 Patch #1551: use [@@noalloc] instead of "noalloc" for recent enough v… Dec 10, 2016

The Zarith library


This library implements arithmetic and logical operations over arbitrary-precision integers.

The module is simply named Z. Its interface is similar to that of the Int32, Int64 and Nativeint modules from the OCaml standard library, with some additional functions. See the file z.mlip for documentation.

The implementation uses GMP (the GNU Multiple Precision arithmetic library) to compute over big integers. However, small integers are represented as unboxed Caml integers, to save space and improve performance. Big integers are allocated in the Caml heap, bypassing GMP's memory management and achieving better GC behavior than e.g. the MLGMP library. Computations on small integers use a special, faster path (coded in assembly for some platforms and functions) eschewing calls to GMP, while computations on large intergers use the low-level MPN functions from GMP.

Arbitrary-precision integers can be compared correctly using OCaml's polymorphic comparison operators (=, <, >, etc.). This requires OCaml version 3.12.1 or later, though.

Additional features include:

  • a module Q for rationals, built on top of Z (see q.mli)
  • a compatibility layer Big_int_Z that implements the same API as Big_int from the legacy Num library, but uses Z internally


  • OCaml, preferably version 3.12.1 or later. (Earlier versions are usable but generic comparisons will misbehave.)
  • Either the GMP library or the MPIR library, including development files.
  • GCC or Clang or a gcc-compatible C compiler and assembler (other compilers may work).
  • The Perl programming language.
  • The Findlib package manager (optional, recommended).


  1. First, run the "configure" script by typing:

The configure script has a few options. Use the -help option to get a list and short description of each option.

  1. It creates a Makefile, which can be invoked by:

This builds native and bytecode versions of the library.

  1. The libraries are installed by typing:
   make install

or, if you install to a system location but are not an administrator

   sudo make install

If Findlib is detected, it is used to install files. Otherwise, the files are copied to a zarith/ subdirectory of the directory given by ocamlc -where.

The libraries are named zarith.cmxa and zarith.cma, and the Findlib module is named zarith.

Compiling and linking with the library requires passing the -I +zarith option to ocamlc / ocamlopt, or the -package zarith option to ocamlfind.

  1. (optional, recommended) Test programs are built and run by the additional command
  make tests

(but these are not installed).

  1. (optional) HTML API documentation is built (using ocamldoc) by the additional command
  make doc


The documentation for the latest release is hosted on GitHub Pages.


This Library is distributed under the terms of the GNU Library General Public License version 2, with a special exception allowing unconstrained static linking. See LICENSE file for details.


  • Antoine Miné, Université Pierre et Marie Curie, formerly ENS Paris.
  • Xavier Leroy, INRIA Paris-Rocquencourt.
  • Pascal Cuoq, CEA LIST.
  • Christophe Troestler (toplevel module)


Copyright (c) 2010-2011 Antoine Miné, Abstraction project. Abstraction is part of the LIENS (Laboratoire d'Informatique de l'ENS), a joint laboratory by: CNRS (Centre national de la recherche scientifique, France), ENS (École normale supérieure, Paris, France), INRIA Rocquencourt (Institut national de recherche en informatique, France).


Source files Description
configure configuration script
caml_z.c C implementation of all functions
caml_z_*.S asm implementation for a few functions script to generate[i] from[i]p[i]p templates used to generate[i]p[i] wrapper to provide a Big_int compatible API to Z[i] rational library, pure OCaml on top of Z toplevel module to provide pretty-printing
projet.mak builds Z, Q and the tests
tests/ simple regression tests and benchmarks

Note: simply scans the asm file (if any) to see which functions have an asm implementation. It then fixes the external statements in .mlp and .mlip accordingly. The argument to is the suffix * of the caml_z_*.S to use (guessed by configure).

You can’t perform that action at this time.