GNU Multiple Precision Arithmetic Library
gmp
|
|
---|---|
Basic data
|
|
developer | Free Software Foundation |
Publishing year | 1991 |
Current version | 6.2.0 (January 17, 2020) |
operating system | Unix , GNU / Linux |
programming language | C , assembly language |
category | Standard C Library |
License | LGPL |
www.gmplib.org |
The GNU Multiple Precision Arithmetic Library ( GMP ) is a programming library that implements arithmetic functions for arbitrarily precise / large numbers. The first version of GMP appeared in 1991 . Since then, the library has been continuously expanded and improved and published in an annual release . GMP is an official part of the GNU project , is under the LGPL and is therefore free software . Computer algebra systems that use GMP include Maple and Mathematica .
Restrictions
The possibilities of GMP with regard to the size of the numbers are only limited by the RAM or virtual memory available in the computer . In spite of the emulation of the hardware calculations in the form of software algorithms, GMP remains relatively fast, since in many places it was optimized with the help of assembler instructions .
Range of functions
The functional scope of GMP is divided into seven categories.
- Arithmetic and logical functions for signed integers (approx. 140 functions)
- Arithmetic functions for rational numbers (approx. 35 functions)
- Arithmetic functions for floating point numbers (approx. 65 functions)
- C ++ wrapper classes for the above functions
- Arithmetic functions for unsigned integers for which the user must implement memory management himself
- Functions for rounding floating point numbers
- Functions for compatibility with the Berkeley MP project
Example of usage
GMP has three main data types: mpz_t
for any large integers , mpf_t
for any large floating point numbers with changeable, also extremely high precision and mpq_t
for the representation of numbers as fractions . The GMP variables cannot simply be assigned values as with normal data types, but special functions must be called (see listing). The following source code illustrates the basic use of GMP:
#include <gmp.h>
int main(void)
{
mpz_t a; // Deklariere GMP-Ganzzahlvariable
mpf_t b; // Deklariere GMP-Fließkommavariable
mpq_t c; // Deklariere GMP-Bruchvariable
mpz_init(a); // Initialisiere GMP-Ganzzahlvariable
mpf_init(b); // Initialisiere GMP-Fließkommavariable
mpq_init(c); // Initialisiere GMP-Bruchvariable
mpz_set_ui(a,1337); // Setze GMP-Ganzzahlvariable auf einen unsigned-integer-Wert
mpz_set_str(a,"4242424242",10); // Andere Moeglichkeit, eine MPZ-Variable zu setzen, z. B.
// falls der Wertebereich von unsigned int zu klein ist. 10 ist die Basis
mpf_set_d(b,3.14159265358); // GMP-Fließkommavariable auf einen double-Wert setzen
mpf_set_str(b,"3.141592653589793238462643383279502",10); // Wie mpz_set_str();
mpq_set_ui(c,23423,11123); // Setze c auf den Wert des Bruchs (23423/11123)
mpq_canonicalize(c); // Muss durchgefuehrt werden, um gemeinsame Teiler zu entfernen
// und die Vorzeichen zu berichtigen
return 0;
}
Important functions of GMP include:
#include <gmp.h>
int main(void)
{
// Die vorher deklarierten Variablen seien geltend
// Weitestgehend identische Funktionen sind auch fuer mpf und mpq verfuegbar (einfach Praefix austauschen)
mpz_t d,e;
mpz_init_set_str(d,"133742",10); // Kombinierte Initialisierungs- und Zuweisungsfunktion
mpz_init(e);
mpz_add(e,a,d); // a und d addieren und das Ergebnis der Variable e zuweisen
gmp_printf("%Zd\n",e); // gmp_printf() ist aequivalent zu printf(), gibt nur eben GMP-Variablen aus
mpz_mul(a,e,d); // e und d multiplizieren und a zuweisen
mpz_add_ui(d,a,421337); // a und den unsigned-long-int-Wert 421337 addieren und d zuweisen
return 0;
}
Web links
- Official GMP website including documentation in HTML and PDF
- Official website of the GNU project
- MPIR - A fork of libGMP to better support Microsoft Windows
Individual evidence
- ↑ gmplib.org . " The first GMP release was made in 1991. "
- ↑ Changes in GMP 6.2.0 ( page no longer available , search in web archives ) Info: The link was automatically marked as defective. Please check the link according to the instructions and then remove this notice.
- ^ The GNU Multiple Precision (GMP) Library. Accessed March 1, 2017.
- ^ The Mathematica Kernel: Issues in the Design and Implementation. Lecture text. Accessed November 14, 2016.