Software:GNU Multiple Precision Arithmetic Library: Difference between revisions

From HandWiki
imported>HamTop
change
 
Wincert (talk | contribs)
simplify
 
Line 1: Line 1:
{{Short description|Free software}}
{{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]] and [[Software:GNU General Public License|GPLv2]]<ref name=what/>
| license = Dual [[Software:GNU Lesser General Public License|LGPLv3]] [[Software:GNU General Public License|GPLv2]]<ref name=what/>
| website = {{URL|https://gmplib.org}}
| repo = {{URL|https://gmplib.org/repo/}}
}}
}}


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]], [[Organization:PHP|PHP]], [[Python (programming language)|Python]], [[R (programming language)|R]], [[Ruby (programming language)|Ruby]], and [[Rust (programming language)|Rust]]. Prior to 2008, [[Software:Kaffe|Kaffe]], a [[Java virtual machine]], used GMP to support Java built-in arbitrary precision arithmetic.<ref>{{cite web
| 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 bignum library for all operand sizes. Some important factors in doing this are:
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 arithmatic.
* 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]] (specialized for different [[Central processing unit|processors]]) are used in the most common inner loops to optimize them as much as possible.
* [[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 [[Software:Mathematica|Mathematica]]<ref>{{cite web
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>


== Examples ==
== 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:


<source lang="C">
<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_t x, y, result;


  mpz_init_set_str(x, "7612058254738945", 10);
    mpz_init_set_str(x, "7612058254738945", 10);
  mpz_init_set_str(y, "9263591128439081", 10);
    mpz_init_set_str(y, "9263591128439081", 10);
  mpz_init(result);
    mpz_init(result);


  mpz_mul(result, x, y);
    mpz_mul(result, x, y);
  gmp_printf("    %Zd\n"
    gmp_printf(
            "*\n"
        "    %Zd\n"
            "    %Zd\n"
        "*\n"
            "--------------------\n"
        "    %Zd\n"
            "%Zd\n", x, y, result);
        "--------------------\n"
        "%Zd\n",
        x, y, result
    );


  /* free used memory */
    // free used memory
  mpz_clear(x);
    mpz_clear(x);
  mpz_clear(y);
    mpz_clear(y);
  mpz_clear(result);
    mpz_clear(result);


  return 0;
    return 0;
}
}
</source>
</syntaxhighlight>


This code calculates the value of 7612058254738945 × 9263591128439081.
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.)


<source lang="text">
<syntaxhighlight lang="text">
     7612058254738945
     7612058254738945
*
     9263591128439081
     9263591128439081
--------------------
--------------------
70514995317761165008628990709545
70514995317761165008628990709545
</source>
</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.)


<source lang="cpp">
<syntaxhighlight lang="cpp">
#include <iostream>
import <gmpxx.h>;
#include <gmpxx.h>
import std;


int main() {
int main() {
  mpz_class x("7612058254738945");
    mpz_class x("7612058254738945");
  mpz_class y("9263591128439081");
    mpz_class y("9263591128439081");
    mpz_class result = x * y;


  std::cout << "    " << x << "\n"
    std::println(
            << "*\n"
        "    {}\n"
            << "    " << y << "\n"
        "*\n"
            << "--------------------\n"
        "    {}\n"
            << x * y << "\n";
        "--------------------\n"
        "{}",
        x.get_str(), y.get_str(), result.get_str()
    );


  return 0;
    return 0;
}
}
</source>
</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]
| [[Organization:PHP|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 Domain
| Public domain
|-
|-
| [http://chgmp.sourceforge.net/ Ch GMP]
| [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]
| [[BMDFM|BMDFM LISP / C]]
| [[Binary Modular Dataflow Machine|BMDFM LISP C]]
| Public Domain
| Public domain
|-
|-
| [[Software:Glasgow Haskell Compiler|Glasgow Haskell Compiler]]<br /><small>(The implementation of <code>Integer</code><br />is basically a binding to GMP)</small>
| [[Software:Glasgow Haskell Compiler|Glasgow Haskell Compiler]] (The implementation of <code>Integer</code> is basically a binding to GMP)
| [[Haskell (programming language)|Haskell]]
| [[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 (programming language)|LuaJIT]]
| [[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 (programming language)|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]] – a library for arbitrary-precision computations with correct rounding, based on GNU MP
* [[Software:GNU MPFR|GNU MPFR]] – library for arbitrary-precision computations with correct rounding, based on GNU MP
* [[Software:Class Library for Numbers|CLN]] – a class library for arbitrary precision  
* [[Software:Class Library for Numbers|CLN]] – class library for arbitrary precision  
* [[Software:MPIR (mathematics software)|MPIR]] – a fork of GMP, not maintained any more
* [[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}}
== External links ==


{{GNU}}
{{GNU}}


{{DEFAULTSORT:Gnu Multi-Precision Library}}
{{DEFAULTSORT:Gnu Multi-Precision Library}}
[[Category:Assembly language software]]
[[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

GNU Multiple Precision Arithmetic Library
Developer(s)GNU Project
Initial release1991; 35 years ago (1991)[1]
Stable release6.2.1 (14 November 2020; 5 years ago (2020-11-14)[2]) [±]
Written inC, (C++, assembly optionally)
TypeMathematical software
LicenseDual LGPLv3GPLv2[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 7612058254738945×9263591128439081.

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 to mpz_t)
  • mpq_class (corresponds to mpq_t)
  • mpf_class (corresponds to mpf_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

References