Software:GNU Multiple Precision Arithmetic Library: Difference between revisions
imported>HamTop change |
simplify |
||
| Line 1: | Line 1: | ||
{{Infobox software | {{Infobox software | ||
| name = GNU Multiple Precision Arithmetic Library | | name = GNU Multiple Precision Arithmetic Library | ||
| Line 16: | Line 15: | ||
| programming language = [[C (programming language)|C]], ([[C++]], [[Assembly language|assembly]] optionally) | | programming language = [[C (programming language)|C]], ([[C++]], [[Assembly language|assembly]] optionally) | ||
| genre = [[Software:Mathematical software|Mathematical software]] | | genre = [[Software:Mathematical software|Mathematical software]] | ||
| license = Dual [[Software:GNU Lesser General Public License|LGPLv3]] | | license = Dual [[Software:GNU Lesser General Public License|LGPLv3]] – [[Software:GNU General Public License|GPLv2]]<ref name=what/> | ||
}} | }} | ||
| Line 33: | Line 30: | ||
| url=https://gmplib.org/gmp6.0.html | | url=https://gmplib.org/gmp6.0.html | ||
| title=GMP 6.0 News | | title=GMP 6.0 News | ||
| access-date=2019-10-04}}</ref> GMP has a rich set of functions, and the functions have a regular interface. The basic interface is for [[C (programming language)|C]], but [[Wrapper function|wrappers]] exist for other languages, including [[Ada (programming language)|Ada]], [[C++]], [[C Sharp (programming language)|C#]], [[Julia (programming language)|Julia]], .NET, [[OCaml]], [[Perl]], [[ | | access-date=2019-10-04}}</ref> GMP has a rich set of functions, and the functions have a regular interface. The basic interface is for [[C (programming language)|C]], but [[Wrapper function|wrappers]] exist for other languages, including [[Ada (programming language)|Ada]], [[C++]], [[C Sharp (programming language)|C#]], [[Julia (programming language)|Julia]], [[.NET|.NET]], [[OCaml]], [[Perl]], [[PHP]], [[Python (programming language)|Python]], [[R (programming language)|R]], [[Ruby (programming language)|Ruby]], and [[Rust (programming language)|Rust]]. | ||
| url=http://www.kaffe.org/pipermail/kaffe/2008-February/191039.html | |||
| title=Removed GMP math? | Before 2008, [[Software:Kaffe|Kaffe]], a [[Java virtual machine]], used GMP to support Java built-in arbitrary precision arithmetic.<ref>{{cite web | ||
| last=Hughes | |url=http://www.kaffe.org/pipermail/kaffe/2008-February/191039.html | ||
| first=Andrew John | |title=Removed GMP math? | ||
| date=2008-02-28 | |last=Hughes | ||
| access-date=2013-03-17}}</ref> Shortly after, GMP support was added to [[Software:GNU Classpath|GNU Classpath]].<ref>{{cite web | |first=Andrew John | ||
| url=https://www.gnu.org/software/classpath/announce/20090205.html | |date=2008-02-28 | ||
| title=GNU Classpath 0.98 "Better Late Than Never" | |access-date=2013-03-17}}</ref> Shortly after, GMP support was added to [[Software:GNU Classpath|GNU Classpath]], as the backends to <code>java.math.BigInteger</code> and <code>java.math.BigDecimal</code>.<ref>{{cite web | ||
| date=2009-02-05 | |url=https://www.gnu.org/software/classpath/announce/20090205.html | ||
| access-date=2013-03-17}}</ref> | |title=GNU Classpath 0.98 "Better Late Than Never" | ||
|date=2009-02-05 | |||
|access-date=2013-03-17}}</ref> | |||
The main target applications of GMP are [[Cryptography|cryptography]] applications and research, Internet security applications, and [[Software:Computer algebra system|computer algebra system]]s. | The main target applications of GMP are [[Cryptography|cryptography]] applications and research, Internet security applications, and [[Software:Computer algebra system|computer algebra system]]s. | ||
GMP aims to be faster than any other | GMP aims to be faster than any other [[Arbitrary-precision arithmetic|arbitrary-precision arithmetic]] (big number) library for all operand sizes. Some important factors in doing this are: | ||
* Full words are the basic type for all | * Full words are the basic type for all arithmetic. | ||
* Different [[Algorithm|algorithm]]s are used for different [[Operand|operand]] sizes; algorithms which are more efficient with large numbers are not used when dealing with small numbers. | * Different [[Algorithm|algorithm]]s are used for different [[Operand|operand]] sizes; algorithms which are more efficient with large numbers are not used when dealing with small numbers. | ||
* [[Assembly language | * [[Assembly language]] (specialized for different [[Central processing unit|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.<ref name=main>{{cite web | The first GMP release was made in 1991. It is constantly developed and maintained.<ref name=main>{{cite web | ||
| Line 59: | Line 58: | ||
GMP is part of the [[Software:GNU|GNU]] project (although its website being off gnu.org may cause confusion), and is distributed under the [[Software:GNU Lesser General Public License|GNU Lesser General Public License]] (LGPL). | GMP is part of the [[Software:GNU|GNU]] project (although its website being off gnu.org may cause confusion), and is distributed under the [[Software:GNU Lesser General Public License|GNU Lesser General Public License]] (LGPL). | ||
GMP is used for integer arithmetic in many [[Software:Computer algebra system|computer algebra system]]s such as [[ | GMP is used for integer arithmetic in many [[Software:Computer algebra system|computer algebra system]]s such as [[Company:Mathematica|Mathematica]]<ref>{{cite web | ||
| url=https://library.wolfram.com/infocenter/Conferences/7518/Macalester_talk.txt | | url=https://library.wolfram.com/infocenter/Conferences/7518/Macalester_talk.txt | ||
| title=The Mathematica Kernel: Issues in the Design and Implementation | | title=The Mathematica Kernel: Issues in the Design and Implementation | ||
| Line 66: | Line 65: | ||
| url=https://www.maplesoft.com/support/help/AddOns/view.aspx?path=GMP | | url=https://www.maplesoft.com/support/help/AddOns/view.aspx?path=GMP | ||
| title= The GNU Multiple Precision (GMP) Library | | title= The GNU Multiple Precision (GMP) Library | ||
| publisher=Maplesoft | | publisher=[[Maplesoft]] | ||
| access-date=2013-03-17}}</ref> It is also used in the Computational Geometry Algorithms Library (CGAL). | | access-date=2013-03-17}}</ref> It is also used in the Computational Geometry Algorithms Library (CGAL). | ||
| Line 75: | Line 74: | ||
| access-date=2013-03-17}}</ref> | | access-date=2013-03-17}}</ref> | ||
== | == C library interface == | ||
The C library interface defines: | |||
* <code>mpz_t</code> (multiprecision integer) | |||
* <code>mpq_t</code> (multiprecision rational) | |||
* <code>mpf_t</code> (multiprecision floating-point number) | |||
* <code>gmp_randstate_t</code> (random state, used for producing random numbers) | |||
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. | |||
=== Example === | |||
Here is an example of C code showing the use of the GMP library to multiply and print large numbers: | Here is an example of C code showing the use of the GMP library to multiply and print large numbers: | ||
< | <syntaxhighlight lang="C"> | ||
#include <stdio.h> | #include <stdio.h> | ||
#include <gmp.h> | #include <gmp.h> | ||
int main(void) { | 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; | |||
} | } | ||
</ | </syntaxhighlight> | ||
This code calculates the value of 7612058254738945 | This code calculates the value of <math>7612058254738945 \times 9263591128439081</math>. | ||
Compiling and running this program gives this result. (The <code>-lgmp</code> flag is used if compiling on Unix-type systems.) | Compiling and running this program gives this result. (The <code>-lgmp</code> flag is used if compiling on Unix-type systems.) | ||
< | <syntaxhighlight lang="text"> | ||
7612058254738945 | 7612058254738945 | ||
* | |||
9263591128439081 | 9263591128439081 | ||
-------------------- | -------------------- | ||
70514995317761165008628990709545 | 70514995317761165008628990709545 | ||
</ | </syntaxhighlight> | ||
== C++ library interface == | |||
The C++ library interface defines the classes: | |||
* <code>mpz_class</code> (corresponds to <code>mpz_t</code>) | |||
* <code>mpq_class</code> (corresponds to <code>mpq_t</code>) | |||
* <code>mpf_class</code> (corresponds to <code>mpf_t</code>) | |||
* <code>gmp_randclass</code> (offers random number utilities) | |||
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.<ref>{{Cite web|title=12.6 C++ Interface Limitations|url=https://gmplib.org/manual/C_002b_002b-Interface-Limitations|author=GNU Multiple Precision Arithmetic Library|website=gmplib.org|publisher=GNU Project}}</ref> 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>). | |||
=== Example === | |||
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.) | 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.) | ||
< | <syntaxhighlight lang="cpp"> | ||
import <gmpxx.h>; | |||
import std; | |||
int main() { | int main() { | ||
mpz_class x("7612058254738945"); | |||
mpz_class y("9263591128439081"); | |||
mpz_class result = x * y; | |||
std::println( | |||
" {}\n" | |||
"*\n" | |||
" {}\n" | |||
"--------------------\n" | |||
"{}", | |||
x.get_str(), y.get_str(), result.get_str() | |||
); | |||
return 0; | |||
} | } | ||
</ | </syntaxhighlight> | ||
== Language bindings == | == Language bindings == | ||
| Line 173: | Line 198: | ||
|- | |- | ||
| [https://www.php.net/gmp GNU Multi-Precision Library for PHP] | | [https://www.php.net/gmp GNU Multi-Precision Library for PHP] | ||
| [[ | | [[PHP]] | ||
| [[Software:PHP License|PHP]] | | [[Software:PHP License|PHP]] | ||
|- | |- | ||
| [http://www.math.uni.wroc.pl/~hebisch/prog/ GNU Multi-Precision Routines for SBCL] | | [http://www.math.uni.wroc.pl/~hebisch/prog/ GNU Multi-Precision Routines for SBCL] {{Webarchive|url=https://web.archive.org/web/20201119052521/http://www.math.uni.wroc.pl/~hebisch/prog/ |date=2020-11-19}} | ||
| [[Common Lisp]] | | [[Common Lisp]] | ||
| Public | | Public domain | ||
|- | |- | ||
| [ | | [https://chgmp.sourceforge.net/ Ch GMP] | ||
| [[Software:Ch (computer programming)|Ch]] | | [[Software:Ch (computer programming)|Ch]] | ||
| [[Proprietary software|Proprietary]] | | [[Proprietary software|Proprietary]] | ||
|- | |- | ||
| [http://bmdfm.com/ Parallel GMP Wrapper for BMDFM] | | [http://bmdfm.com/ Parallel GMP Wrapper for BMDFM] | ||
| [[ | | [[Binary Modular Dataflow Machine|BMDFM LISP – C]] | ||
| Public | | Public domain | ||
|- | |- | ||
| [[Software:Glasgow Haskell Compiler|Glasgow Haskell Compiler]] | | [[Software:Glasgow Haskell Compiler|Glasgow Haskell Compiler]] (The implementation of <code>Integer</code> is basically a binding to GMP) | ||
| [[ | | [[Haskell]] | ||
| [[Software:BSD licenses|BSD]] | | [[Software:BSD licenses|BSD]] | ||
|- | |- | ||
| [https://github.com/Playermet/luajit-gmp luajit-gmp] | | [https://github.com/Playermet/luajit-gmp luajit-gmp] | ||
| [[Lua | | [[Software:Lua|LuaJIT]] | ||
| [[Software:MIT License|MIT]] | | [[Software:MIT License|MIT]] | ||
|- | |- | ||
| [https://code.google.com/archive/p/gmp-wrapper-for-delphi gmp-wrapper-for-delphi] | | [https://code.google.com/archive/p/gmp-wrapper-for-delphi gmp-wrapper-for-delphi] | ||
| [[Delphi | | [[Software:Delphi|Delphi]] | ||
| [[Software:MIT License|MIT]] | | [[Software:MIT License|MIT]] | ||
|- | |- | ||
| Line 205: | Line 230: | ||
|- | |- | ||
| [https://github.com/MachineCognitis/Math.Gmp.Native Math.Gmp.Native Library] | | [https://github.com/MachineCognitis/Math.Gmp.Native Math.Gmp.Native Library] | ||
| .NET | | [[.NET|.NET]] | ||
| [[Software:MIT License|MIT]] | | [[Software:MIT License|MIT]] | ||
|- | |- | ||
| Line 218: | Line 243: | ||
== See also == | == See also == | ||
* [[Software:GNU MPFR|GNU MPFR]] – | * [[Software:GNU MPFR|GNU MPFR]] – library for arbitrary-precision computations with correct rounding, based on GNU MP | ||
* [[Software:Class Library for Numbers|CLN]] – | * [[Software:Class Library for Numbers|CLN]] – class library for arbitrary precision | ||
* [[Software:MPIR (mathematics software)|MPIR]] – | * [[Software:List of free and open-source software packages#Mathematical libraries|List of open-source mathematical libraries]] | ||
* [[Software:MPIR (mathematics software)|MPIR]] – fork of GMP, unmaintained | |||
== References == | == References == | ||
{{Reflist}} | {{Reflist}} | ||
{{GNU}} | {{GNU}} | ||
{{DEFAULTSORT:Gnu Multi-Precision Library}} | {{DEFAULTSORT:Gnu Multi-Precision Library}} | ||
[[Category:Computer arithmetic]] | [[Category:Computer arithmetic]] | ||
[[Category:Free software programmed in C]] | [[Category:Free software programmed in C]] | ||
[[Category:GNU Project software|Multiple Precision Arithmetic Library]] | [[Category:GNU Project software|Multiple Precision Arithmetic Library]] | ||
[[Category:Numerical libraries]] | [[Category:Numerical libraries]] | ||
<!-- Hidden categories below --> | |||
[[Category:Articles with example C code]] | |||
{{Sourceattribution|GNU Multiple Precision Arithmetic Library}} | {{Sourceattribution|GNU Multiple Precision Arithmetic Library}} | ||
Latest revision as of 20:47, 11 April 2026
| Developer(s) | GNU Project |
|---|---|
| Initial release | 1991[1] |
| Stable release | 6.2.1 (14 November 2020[2]) [±] |
| Written in | C, (C++, assembly optionally) |
| Type | Mathematical software |
| License | Dual LGPLv3 – GPLv2[3] |
GNU Multiple Precision Arithmetic Library (GMP) is a free library for arbitrary-precision arithmetic, operating on signed integers, rational numbers, and floating-point numbers.[3] 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).[4][5] 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.[6] Shortly after, GMP support was added to GNU Classpath, as the backends to java.math.BigInteger and java.math.BigDecimal.[7]
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:
- 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.[8]
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[9] and Maple.[10] It is also used in the Computational Geometry Algorithms Library (CGAL).
GMP is needed to build the GNU Compiler Collection (GCC).[11]
C library interface
The C library interface defines:
mpz_t(multiprecision integer)mpq_t(multiprecision rational)mpf_t(multiprecision floating-point number)gmp_randstate_t(random state, used for producing random numbers)
Functions are prefixed with the type name (for example, operations on multiprecision integers are prefixed with mpz, etc.)
The library also provides additional utilities (all prefixed with gmp), such as gmp_scanf, gmp_printf, etc.
Example
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 .
Compiling and running this program gives this result. (The -lgmp flag is used if compiling on Unix-type systems.)
7612058254738945
*
9263591128439081
--------------------
70514995317761165008628990709545
C++ library interface
The C++ library interface defines the classes:
mpz_class(corresponds tompz_t)mpq_class(corresponds tompq_t)mpf_class(corresponds tompf_t)gmp_randclass(offers random number utilities)
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 auto in declarations.[12] For returning to the C type, each class offers a corresponding get_mp_t() method (for example mpz_class::get_mpz_t()).
Example
For comparison, one can write instead the following equivalent C++ program. (The -lgmpxx -lgmp flags are used if compiling on Unix-type systems.)
import <gmpxx.h>;
import std;
int main() {
mpz_class x("7612058254738945");
mpz_class y("9263591128439081");
mpz_class result = x * y;
std::println(
" {}\n"
"*\n"
" {}\n"
"--------------------\n"
"{}",
x.get_str(), y.get_str(), result.get_str()
);
return 0;
}
Language bindings
| Library name | Language | License |
|---|---|---|
| GNU Multi-Precision Library | C, C++ | LGPL |
| Math::GMP | Perl | LGPL |
| Math::GMPz, Math::GMPf and Math::GMPq | Perl | Artistic License v1.0 + GPL v1.0-or-later |
| General Multiprecision Python Project | Python | LGPL |
| R package 'gmp' | R | GPL |
| The RubyGems project | Ruby | Apache 2.0 |
| Rust FFI bindings for GMP, MPFR and MPC | Rust | LGPL |
| GNU Multi-Precision Library for PHP | PHP | PHP |
| GNU Multi-Precision Routines for SBCL | Common Lisp | Public domain |
| Ch GMP | Ch | Proprietary |
| Parallel GMP Wrapper for BMDFM | BMDFM LISP – C | Public domain |
Glasgow Haskell Compiler (The implementation of Integer is basically a binding to GMP)
|
Haskell | BSD |
| luajit-gmp | LuaJIT | MIT |
| gmp-wrapper-for-delphi | Delphi | MIT |
| Zarith | OCaml | LGPL |
| Math.Gmp.Native Library | .NET | MIT |
| nim-gmp | Nim | MIT |
| JGMP | Java | LGPL |
See also
- GNU MPFR – library for arbitrary-precision computations with correct rounding, based on GNU MP
- CLN – class library for arbitrary precision
- List of open-source mathematical libraries
- MPIR – fork of GMP, unmaintained
References
- ↑ "GNU MP archive". https://gmplib.org/download/gmp/archive/.
- ↑ V6.2.1 - "The GNU MP Bignum Library". https://gmplib.org/.
- ↑ 3.0 3.1 "What is GMP?". https://gmplib.org/#WHAT.
- ↑ Granlund, Torbjorn (2009-07-06). "Problems with mpz_set_str and huge strings". https://gmplib.org/list-archives/gmp-bugs/2009-July/001538.html.
- ↑ "GMP 6.0 News". https://gmplib.org/gmp6.0.html.
- ↑ Hughes, Andrew John (2008-02-28). "Removed GMP math?". http://www.kaffe.org/pipermail/kaffe/2008-February/191039.html.
- ↑ "GNU Classpath 0.98 "Better Late Than Never"". 2009-02-05. https://www.gnu.org/software/classpath/announce/20090205.html.
- ↑ "GNU MP Bignum Library". https://gmplib.org/.
- ↑ "The Mathematica Kernel: Issues in the Design and Implementation". October 2006. https://library.wolfram.com/infocenter/Conferences/7518/Macalester_talk.txt.
- ↑ "The GNU Multiple Precision (GMP) Library". Maplesoft. https://www.maplesoft.com/support/help/AddOns/view.aspx?path=GMP.
- ↑ 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. https://gcc.gnu.org/gcc-4.3/changes.html#mpfropts.
- ↑ GNU Multiple Precision Arithmetic Library. "12.6 C++ Interface Limitations". GNU Project. https://gmplib.org/manual/C_002b_002b-Interface-Limitations.
