GNU Multiple Precision Arithmetic Library
GNU Multiple Precision Arithmetic Library 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. 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. Prior to 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.
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 bignum library for all operand sizes. Some important factors in doing this are:
- Full words are the basic type for all arithmetic.
- Different algorithms are used for different operand sizes; algorithms which are more efficient with large numbers are not used when dealing with small numbers.
- Assembly language is used in the most common inner loops to optimize them as much as possible.
GMP is part of the GNU project, and is distributed under the GNU Lesser General Public License.
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.
GMP is needed to build the GNU Compiler Collection.
Examples
Here is an example of C code showing the use of the GMP library to multiply and print large numbers:- include
- include
This code calculates the value of 7612058254738945 × 9263591128439081.
Compiling and running this program gives this result.
7612058254738945
9263591128439081
--------------------
70514995317761165008628990709545
For comparison, one can write instead the following equivalent C++ program.
- include
- include