| GNU Multiple Precision Arithmetic Library | |
|---|---|
| Developer | GNU Project |
| Initial release | 1991; 34 years ago (1991)[1] |
| Stable release | |
| Repository | |
| Written in | C, (C++,assembly optionally) |
| Type | Mathematical software |
| License | DualLGPLv3 andGPLv2[4] |
| Website | gmplib |
GNU Multiple Precision Arithmetic Library (GMP) is afree library forarbitrary-precision arithmetic, operating onsignedintegers,rational numbers, andfloating-point numbers.[4] There are no practical limits to the precision except the ones implied by the availablememory (operands may be of up to 232−1 bits on 32-bit machines and 237 bits on 64-bit machines).[5][6] GMP has a rich set of functions, and the functions have a regular interface. The basic interface is forC, butwrappers exist for other languages, includingAda,C++,C#,Julia,.NET,OCaml,Perl,PHP,Python,R,Ruby, andRust. Prior to 2008,Kaffe, aJava virtual machine, used GMP to support Java built-in arbitrary precision arithmetic.[7] Shortly after, GMP support was added toGNU Classpath.[8]
The main target applications of GMP arecryptography applications and research, Internet security applications, andcomputer algebra systems.
GMP aims to be faster than any otherbignum 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.[9]
GMP is part of theGNU project (although its website being off gnu.org may cause confusion), and is distributed under theGNU Lesser General Public License (LGPL).
GMP is used for integer arithmetic in manycomputer algebra systems such asMathematica[10] andMaple.[11] It is also used in theComputational Geometry Algorithms Library (CGAL).
GMP is needed to build theGNU Compiler Collection (GCC).[12]
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>intmain(void){mpz_tx,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);return0;}
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>intmain(){mpz_classx("7612058254738945");mpz_classy("9263591128439081");std::cout<<" "<<x<<"\n"<<"*\n"<<" "<<y<<"\n"<<"--------------------\n"<<x*y<<"\n";return0;}