GNU Multiple Precision Arithmetic Library

The GNU Multiple Precision Arithmetic Library is a programming library that implements arithmetic functions for arbitrary precision / large numbers. The first version of GMP appeared in 1991. Since the library is constantly being expanded and improved and edited in an annual release. GMP is an official part of the GNU project, is under the LGPL license and is thus free software.


The possibilities of GMP in relation to the size of the numbers is unique and limited only by RAM or virtual memory available to the computer. Despite the emulation of the hardware calculations in the form of software algorithms GMP remains relatively quickly, as has been optimized at many points with the help of assembler instructions.


Features of GMP is divided into seven categories.

  • Arithmetic and logical functions for signed integer (about 140 functions)
  • Arithmetic on rational numbers (about 35 functions)
  • Arithmetic functions for floating point numbers ( 65 functions)
  • C wrapper classes for the above functions
  • Arithmetic functions for unsigned integers, for which the user must implement the memory management itself
  • Functions on the rounding of floating-point numbers
  • Functions for compatibility with the Berkeley MP project

Example of usage

The GMP has three main types of data: mpz_t ( arbitrarily large integers ), mpf_t ( arbitrarily large floating-point modifiable, even extremely high precision ) and mpq_t ( representation of numbers as a fraction ). The GMP variables can not simply be assigned values ​​( like normal data types), but it need special functions are called (see listing). The following code illustrates the basic use of the GMP:

# Include   int main ( void) {      mpz_t a; / / Declare GMP integer variable      mpf_t b; / / Declare GMP floating point variable      mpq_t c; / / Declare GMP fraction variable        mpz_init (a); / / Initialize GMP integer variable      mpf_init (b); / / Initialize GMP floating point variable      mpq_init (c); / / Initialize GMP fraction variable        mpz_set_ui (a, 1337); / / Set GMP integer variable to an unsigned integer value      mpz_set_str (a, " 4242424242 ", 10); / / Other possibility to set an MPZ variable, eg                                      / / If the value range of unsigned int is too small. 10, the base        mpf_set_d (b, 3.14159265358 ); / / Set the GMP floating point variable to a double value      mpf_set_str ( b " 3.141592653589793238462643383279502 ", 10); / / How mpz_set_str ();        mpq_set_ui ( c 23423.11123 ); / / Set c to the value of the fraction ( 23423/11123 )      mpq_canonicalize (c); / / Must be performed to remove common divisor                                 / / And correct the signs of        return ( 0); } Important functions of the GMP include:

# Include   int main ( void) {      / / The previously declared variables are asserted      / / Largely identical functions are also for mpf and mpq verfuegbar ( simply replace prefix )      mpz_t d, e;      mpz_init_set_str (d, " 133 742 ", 10); / / Combined Initialization and Assignment Function      mpz_init (s);        mpz_add ( e, a, d); And add / / a d and assign the result to the variable e        gmp_printf ("% Zd \ n", e); / / Gmp_printf ( ) is equivalent to printf (), GMP variables are only just out        mpz_mul ( a, e, d); / And multiply d / e and assign a        mpz_add_ui ( d, a, 421337 ); / / A and the unsigned -long- int value 421337 add and assign d } Web Links

  • Official website including GMP documentation in HTML and PDF
  • Official of the GNU Project website
  • MPIR - A Fork of libgmp to better support Microsoft Windows
  • Library (programming)
  • Computer arithmetic
  • GNU