Menu
Home Explore People Places Arts History Plants & Animals Science Life & Culture Technology
On this page
GNU Multiple Precision Arithmetic Library
Free software

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 232−1 bits on 32-bit machines and 237 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. 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 (specialized for different processors) is used in the most common inner loops to optimize them as much as possible.

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).

Related Image Collections Add Image
We don't have any YouTube videos related to GNU Multiple Precision Arithmetic Library yet.
We don't have any PDF documents related to GNU Multiple Precision Arithmetic Library yet.
We don't have any Books related to GNU Multiple Precision Arithmetic Library yet.
We don't have any archived web articles related to GNU Multiple Precision Arithmetic Library yet.

Examples

Here is an example of C code showing the use of the GMP library to multiply and print large numbers:

#include <stdio.h> #include <gmp.h> int main(void) { mpz_t x, y, result; mpz_init_set_str(x, "7612058254738945", 10); mpz_init_set_str(y, "9263591128439081", 10); mpz_init(result); mpz_mul(result, x, y); gmp_printf(" %Zd\n" "*\n" " %Zd\n" "--------------------\n" "%Zd\n", x, y, result); /* free used memory */ mpz_clear(x); mpz_clear(y); mpz_clear(result); return 0; }

This code calculates the value of 7612058254738945 × 9263591128439081.

Compiling and running this program gives this result. (The -lgmp flag is used if compiling on Unix-type systems.)

7612058254738945 * 9263591128439081 -------------------- 70514995317761165008628990709545

For comparison, one can write instead the following equivalent C++ program. (The -lgmpxx -lgmp flags are used if compiling on Unix-type systems.)

#include <iostream> #include <gmpxx.h> int main() { mpz_class x("7612058254738945"); mpz_class y("9263591128439081"); std::cout << " " << x << "\n" << "*\n" << " " << y << "\n" << "--------------------\n" << x * y << "\n"; return 0; }

Language bindings

Library nameLanguageLicense
GNU Multi-Precision LibraryC, C++LGPL
Math::GMPPerlLGPL
Math::GMPz, Math::GMPf and Math::GMPqPerlArtistic License v1.0 + GPL v1.0-or-later
General Multiprecision Python ProjectPythonLGPL
R package 'gmp'RGPL
The RubyGems projectRubyApache 2.0
Rust FFI bindings for GMP, MPFR and MPCRustLGPL
GNU Multi-Precision Library for PHPPHPPHP
GNU Multi-Precision Routines for SBCLCommon LispPublic Domain
Ch GMPChProprietary
Parallel GMP Wrapper for BMDFM BMDFM LISP / CPublic Domain
Glasgow Haskell Compiler(The implementation of Integeris basically a binding to GMP)HaskellBSD
luajit-gmpLuaJITMIT
gmp-wrapper-for-delphiDelphiMIT
ZarithOCamlLGPL
Math.Gmp.Native Library.NETMIT
nim-gmpNimMIT
JGMPJavaLGPL

See also

  • GNU MPFR – a library for arbitrary-precision computations with correct rounding, based on GNU MP
  • CLN – a class library for arbitrary precision
  • MPIR – a fork of GMP, not maintained any more

References

  1. "What is GMP?". Retrieved 2014-04-07. https://gmplib.org/#WHAT

  2. Granlund, Torbjorn (2009-07-06). "Problems with mpz_set_str and huge strings". Retrieved 2013-03-17. https://gmplib.org/list-archives/gmp-bugs/2009-July/001538.html

  3. "GMP 6.0 News". Retrieved 2019-10-04. https://gmplib.org/gmp6.0.html

  4. Hughes, Andrew John (2008-02-28). "Removed GMP math?". Retrieved 2013-03-17. http://www.kaffe.org/pipermail/kaffe/2008-February/191039.html

  5. "GNU Classpath 0.98 "Better Late Than Never"". 2009-02-05. Retrieved 2013-03-17. https://www.gnu.org/software/classpath/announce/20090205.html

  6. "GNU MP Bignum Library". Retrieved 2018-12-03. https://gmplib.org/

  7. "The Mathematica Kernel: Issues in the Design and Implementation". October 2006. Retrieved 2013-03-17. https://library.wolfram.com/infocenter/Conferences/7518/Macalester_talk.txt

  8. "The GNU Multiple Precision (GMP) Library". Maplesoft. Retrieved 2013-03-17. https://www.maplesoft.com/support/help/AddOns/view.aspx?path=GMP

  9. GCC uses the GNU MPFR library, which in turn relies on GMP. "GCC 4.3 Release Series: Changes, New Features, and Fixes". 2012-11-02. Retrieved 2013-03-17. /wiki/GNU_MPFR