GNU Multiple Precision Arithmetic Library

From Infogalactic: the planetary knowledge core
Jump to: navigation, search
GNU Multiple Precision Arithmetic Library
Developer(s) GNU Project
Initial release 1991; 33 years ago (1991)[1]
Stable release 6.1.0 (1 November 2015; 8 years ago (2015-11-01)[2]) [±]
Written in C, (C++, assembly optionally)
Operating system Cross-platform
Type Mathematical software
License Dual LGPLv3 and GPLv2[3]
Website gmplib.org

The 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 in the machine GMP runs on (operand dimension limit is 232-1 bits on 32-bit machines and 237 bits on 64-bit machines).[4] 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#, OCaml, Perl, PHP, Python and R. In the past, the Kaffe Java virtual machine used GMP to support Java built-in arbitrary precision arithmetic. This feature has been removed from recent releases, causing protests from people who claim that they used Kaffe solely for the speed benefits afforded by GMP.[5] As a result, GMP support has been added to GNU Classpath.[6]

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:

The first GMP release was made in 1991. It is constantly developed and maintained.[1]

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[7] and Maple.[8] It is also used in the Computational Geometry Algorithms Library (CGAL) because geometry algorithms tend to 'explode' when using ordinary floating point CPU math.[9]

GMP is needed to build the GNU Compiler Collection (GCC).[10]

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 name Language License
GNU Multi-Precision Library C, C++ LGPL
Math::GMP Perl GPL
General Multiprecision Python Project Python LGPL
R package 'gmp' R GPL
The RubyGems project Ruby Apache 2.0
GNU Multi-Precision Library for PHP PHP PHP
GNU Multi-Precision Routines for SBCL Common Lisp Public Domain
Ch GMP Ch Proprietary
Glasgow Haskell Compiler
(The implementation of Integer
is basically a binding to GMP)
Haskell BSD
luajit-gmp LuaJIT MIT

See also

  • MPFR – library for arbitrary-precision computations with correct rounding, based on GNU MP
  • CLN – a class library for arbitrary precision
  • MPIR – a fork of GMP with mostly compatible interface which aims to provide MSVC-based compilation system for Windows platforms

References

  1. 1.0 1.1 Lua error in package.lua at line 80: module 'strict' not found.
  2. V6.1.0 - Lua error in package.lua at line 80: module 'strict' not found.
  3. 3.0 3.1 Lua error in package.lua at line 80: module 'strict' not found.
  4. Lua error in package.lua at line 80: module 'strict' not found.
  5. Lua error in package.lua at line 80: module 'strict' not found.
  6. Lua error in package.lua at line 80: module 'strict' not found.
  7. Lua error in package.lua at line 80: module 'strict' not found.
  8. Lua error in package.lua at line 80: module 'strict' not found.
  9. Lua error in package.lua at line 80: module 'strict' not found.
  10. GCC uses the MPFR library, which in turn relies on GMP. Lua error in package.lua at line 80: module 'strict' not found.

External links

  • No URL found. Please specify a URL here or add one to Wikidata.