GNU Multiple Precision Arithmetic Library

(diff) ← Previous revision | Latest revision (diff) | Newer revision → (diff)

Template:Short description {{#invoke:Infobox|infobox}}Template:Template other{{#invoke:Check for unknown parameters | check | showblankpositional=1 | unknown = Template:Main other | preview = Page using Template:Infobox software with unknown parameter "_VALUE_"|ignoreblank=y | AsOf | author | background | bodystyle | caption | collapsetext | collapsible | developer | discontinued | engine | engines | genre | included with | language | language count | language footnote | latest preview date | latest preview version | latest release date | latest release version | latest_preview_date | latest_preview_version | latest_release_date | latest_release_version | licence | license | logo | logo alt | logo caption | logo upright | logo size | logo title | logo_alt | logo_caption | logo_upright | logo_size | logo_title | middleware | module | name | operating system | operating_system | other_names | platform | programming language | programming_language | released | replaced_by | replaces | repo | screenshot | screenshot alt | screenshot upright | screenshot size | screenshot title | screenshot_alt | screenshot_upright | screenshot_size | screenshot_title | service_name | size | standard | title | ver layout | website | qid }}Template:Main other

GNU Multiple Precision Arithmetic Library (GMP) is a free library for arbitrary-precision arithmetic, operating on signed integers, rational numbers, and floating-point numbers.<ref name=what>{{#invoke:citation/CS1|citation |CitationClass=web }}</ref> 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).<ref>{{#invoke:citation/CS1|citation |CitationClass=web }}</ref><ref>{{#invoke:citation/CS1|citation |CitationClass=web }}</ref> 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.<ref>{{#invoke:citation/CS1|citation |CitationClass=web }}</ref> Shortly after, GMP support was added to GNU Classpath.<ref>{{#invoke:citation/CS1|citation |CitationClass=web }}</ref>

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.<ref name=main>{{#invoke:citation/CS1|citation |CitationClass=web }}</ref>

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<ref>{{#invoke:citation/CS1|citation |CitationClass=web }}</ref> and Maple.<ref>{{#invoke:citation/CS1|citation |CitationClass=web }}</ref> It is also used in the Computational Geometry Algorithms Library (CGAL).

GMP is needed to build the GNU Compiler Collection (GCC).<ref>GCC uses the GNU MPFR library, which in turn relies on GMP. {{#invoke:citation/CS1|citation |CitationClass=web }}</ref>

ExamplesEdit

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

<syntaxhighlight lang="C">

  1. include <stdio.h>
  2. 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;

} </syntaxhighlight>

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

<syntaxhighlight lang="text">

   7612058254738945
   9263591128439081

70514995317761165008628990709545 </syntaxhighlight>

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

<syntaxhighlight lang="cpp">

  1. include <iostream>
  2. 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;

} </syntaxhighlight>

Language bindingsEdit

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 alsoEdit

  • 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

ReferencesEdit

Template:Reflist

External linksEdit

Template:GNU Template:Authority control