GNU Multiple Precision Arithmetic Library
{{Short description|Free software}}
{{Infobox software
| name = GNU Multiple Precision Arithmetic Library
| logo = GMPLogo.svg
| screenshot =
| caption =
| developer = GNU Project
| released = {{Start date and age|1991}}{{cite web
| url=https://gmplib.org/download/gmp/archive/
| title=GNU MP archive
| access-date=2018-12-03}}
| latest release version = {{wikidata|property|preferred|references|edit|Q1205818|P348|P548=Q2804309}}
| latest release date = {{wikidata|qualifier|preferred|single|Q1205818|P348|P548=Q2804309|P577}}
| latest preview version = {{wikidata|property|preferred|references|edit|Q1205818|P348|P548=Q51930650}}
| latest preview date = {{wikidata|qualifier|preferred|single|Q1205818|P348|P548=Q51930650|P577}}
| programming language = C, (C++, assembly optionally)
| genre = Mathematical software
| license = Dual LGPLv3 and GPLv2
| website = {{URL|https://gmplib.org}}
| repo = {{URL|https://gmplib.org/repo/}}
}}
GNU Multiple Precision Arithmetic Library (GMP) is a free library for arbitrary-precision arithmetic, operating on signed integers, rational numbers, and floating-point numbers.{{cite web
| url=https://gmplib.org/#WHAT
| title=What is GMP?
| access-date=2014-04-07}} 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).{{cite web
| url=https://gmplib.org/list-archives/gmp-bugs/2009-July/001538.html
| title=Problems with mpz_set_str and huge strings
| last=Granlund
| first=Torbjorn
| date=2009-07-06
| access-date=2013-03-17}}{{cite web
| url=https://gmplib.org/gmp6.0.html
| title=GMP 6.0 News
| access-date=2019-10-04}} 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.{{cite web
| url=http://www.kaffe.org/pipermail/kaffe/2008-February/191039.html
| title=Removed GMP math?
| last=Hughes
| first=Andrew John
| date=2008-02-28
| access-date=2013-03-17}} Shortly after, GMP support was added to GNU Classpath.{{cite web
| url=https://www.gnu.org/software/classpath/announce/20090205.html
| title=GNU Classpath 0.98 "Better Late Than Never"
| date=2009-02-05
| access-date=2013-03-17}}
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.{{cite web
| url=https://gmplib.org/
| title=GNU MP Bignum Library
| access-date=2018-12-03}}
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{{cite web
| url=https://library.wolfram.com/infocenter/Conferences/7518/Macalester_talk.txt
| title=The Mathematica Kernel: Issues in the Design and Implementation
| date=October 2006
| access-date=2013-03-17}} and Maple.{{cite web
| url=https://www.maplesoft.com/support/help/AddOns/view.aspx?path=GMP
| title= The GNU Multiple Precision (GMP) Library
| publisher=Maplesoft
| access-date=2013-03-17}} It is also used in the Computational Geometry Algorithms Library (CGAL).
GMP is needed to build the GNU Compiler Collection (GCC).GCC uses the GNU MPFR library, which in turn relies on GMP. {{cite web
| url=https://gcc.gnu.org/gcc-4.3/changes.html#mpfropts
| title=GCC 4.3 Release Series: Changes, New Features, and Fixes
| date=2012-11-02
| access-date=2013-03-17}}
Examples
Here is an example of C code showing the use of the GMP library to multiply and print large numbers:
- include
- include
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
- include
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
class="wikitable" |
Library name
! Language ! License |
---|
[https://gmplib.org/ GNU Multi-Precision Library]
| LGPL |
[https://metacpan.org/pod/Math::GMP Math::GMP]
| Perl | LGPL |
[https://metacpan.org/pod/Math::GMPz Math::GMPz], [https://metacpan.org/pod/Math::GMPf Math::GMPf] and [https://metacpan.org/pod/Math::GMPq Math::GMPq]
| Perl | Artistic License v1.0 + GPL v1.0-or-later |
[https://github.com/aleaxit/gmpy General Multiprecision Python Project]
| Python | LGPL |
[https://cran.r-project.org/web/packages/gmp/index.html R package 'gmp']
| R | GPL |
[https://rubygems.org/gems/gmp The RubyGems project]
| Ruby |
[https://lib.rs/crates/gmp-mpfr-sys Rust FFI bindings for GMP, MPFR and MPC]
| Rust | LGPL |
[https://www.php.net/gmp GNU Multi-Precision Library for PHP]
| PHP | PHP |
[http://www.math.uni.wroc.pl/~hebisch/prog/ GNU Multi-Precision Routines for SBCL] |
[http://chgmp.sourceforge.net/ Ch GMP]
| Ch |
[http://bmdfm.com/ Parallel GMP Wrapper for BMDFM] |
Glasgow Haskell Compiler (The implementation of Integer is basically a binding to GMP) | Haskell | BSD |
[https://github.com/Playermet/luajit-gmp luajit-gmp]
| LuaJIT | MIT |
[https://code.google.com/archive/p/gmp-wrapper-for-delphi gmp-wrapper-for-delphi]
| Delphi | MIT |
[https://github.com/ocaml/Zarith Zarith]
| OCaml | LGPL |
[https://github.com/MachineCognitis/Math.Gmp.Native Math.Gmp.Native Library]
| .NET | MIT |
[https://github.com/FedeOmoto/nim-gmp nim-gmp]
| Nim | MIT |
[https://github.com/jandom-devel/JGMP JGMP]
| Java | LGPL |
See also
References
{{Reflist}}
External links
- {{Official website}}
{{GNU}}
{{Authority control}}
{{DEFAULTSORT:Gnu Multi-Precision Library}}
Category:Assembly language software
Category:C (programming language) libraries
Category:Free software programmed in C
Multiple Precision Arithmetic Library
Category:Software using the GNU Lesser General Public License