GNU Multiple Precision Arithmetic Library (GMP) is a free library for arbitrary-precision arithmetic, operating on signed integers, rational numbers, and floating-point numbers. There are no practical limits to the precision except the ones implied by the available memory (operands may be of up to 2<sup>32</sup>âÂÂ1 bits on 32-bit machines and 2<sup>37</sup> bits on 64-bit machines). GMP has a rich set of functions, and the functions have a regular interface. The basic interface is for C, but wrappers exist for other languages, including Ada, C++, C#, Julia, .NET, OCaml, Perl, PHP, Python, R, Ruby, and Rust.
Before 2008, Kaffe, a Java virtual machine, used GMP to support Java built-in arbitrary precision arithmetic. Shortly after, GMP support was added to GNU Classpath, as the backends to <code>java.math.BigInteger</code> and <code>java.math.BigDecimal</code>.
The main target applications of GMP are cryptography applications and research, Internet security applications, and computer algebra systems.
GMP aims to be faster than any other arbitrary-precision arithmetic (big number) library for all operand sizes. Some important factors in doing this are:
The first GMP release was made in 1991. It is constantly developed and maintained.
GMP is part of the GNU project (although its website being off gnu.org may cause confusion), and is distributed under the GNU Lesser General Public License (LGPL).
GMP is used for integer arithmetic in many computer algebra systems such as Mathematica and Maple. It is also used in the Computational Geometry Algorithms Library (CGAL).
GMP is needed to build the GNU Compiler Collection (GCC).
The C library interface defines:
Functions are prefixed with the type name (for example, operations on multiprecision integers are prefixed with <code>mpz</code>, etc.)
The library also provides additional utilities (all prefixed with <code>gmp</code>), such as <code>gmp_scanf</code>, <code>gmp_printf</code>, etc.
Here is an example of C code showing the use of the GMP library to multiply and print large numbers:
This code calculates the value of .
Compiling and running this program gives this result. (The <code>-lgmp</code> flag is used if compiling on Unix-type systems.)
The C++ library interface defines the classes:
The free-standing functions in the C library are integrated as methods in the C++ classes. The C++ library places all symbols globally, and does not use a library namespace. It is recommended to avoid <code>auto</code> in declarations. For returning to the C type, each class offers a corresponding <code>get_mp_t()</code> method (for example <code>mpz_class::get_mpz_t()</code>).
For comparison, one can write instead the following equivalent C++ program. (The <code>-lgmpxx -lgmp</code> flags are used if compiling on Unix-type systems.)