GNU Multiple Precision Arithmetic Library (lub GMP)GMP library - biblioteka programistyczna udostępniająca liczby całkowite, wymierne i zmiennoprzecinkowe o dowolnej precyzji. Wewnętrzny format danych jest binarny.stackoverflow.com: Write quickly GMP variables in files

Instalacja Zależności

sudo apt-get install m4 Rodzaje kod źródłowy Skopiuj aktualny kod ze strony GMP

Rozpakuj plik

W Ubuntu: Instalacja GMP - Bernard MourrainHow To Install and Use GMP Library by mycodestuff ./configure make make check sudo make install

Libraries have been installed in: /usr/local/lib

If you ever happen to want to link against installed libraries in a given directory, LIBDIR, you must either use libtool, and specify the full pathname of the library, or use the '-LLIBDIR' flag during linking and do at least one of the following: - add LIBDIR to the 'LD_LIBRARY_PATH' environment variable during execution - add LIBDIR to the 'LD_RUN_PATH' environment variable during linking - use the '-Wl,-rpath -Wl,LIBDIR' linker flag - have your system administrator add LIBDIR to '/etc/ld.so.conf'

apt-get

sudo apt-get install libgmp3-dev

rpm / yum

sudo yum install gmp

wersje

gmp_printf("Using GMP-%s \n", gmp_version);

Położenie bibliotek

Szukamy bibliotek: gmp error while compiling mpfr on ubuntu

ls -l /usr/lib/libgmp*

przykładowy wynik:

ls: nie ma dostępu do /usr/lib/libgmp*: Nie ma takiego pliku ani katalogu

ls -l /usr/local/lib/libgmp*

przykładowy wynik: -rw-r--r-- 1 root root 1293344 wrz 23 17:31 /usr/local/lib/libgmp.a -rwxr-xr-x 1 root root 914 wrz 23 17:31 /usr/local/lib/libgmp.la lrwxrwxrwx 1 root root 16 wrz 23 17:31 /usr/local/lib/libgmp.so -> libgmp.so.10.2.0 lrwxrwxrwx 1 root root 16 wrz 23 17:31 /usr/local/lib/libgmp.so.10 -> libgmp.so.10.2.0 -rwxr-xr-x 1 root root 531957 wrz 23 17:31 /usr/local/lib/libgmp.so.10.2.0

dpkg --list | grep gmp

przykładowy wynik:

ii libgmp-dev:amd64 2:5.1.2+dfsg-2ubuntu1 amd64 Multiprecision arithmetic library developers tools ii libgmp10:amd64 2:5.1.2+dfsg-2ubuntu1 amd64 Multiprecision arithmetic library ii libgmp10:i386 2:5.1.2+dfsg-2ubuntu1 i386 Multiprecision arithmetic library ii libgmpxx4ldbl:amd64 2:5.1.2+dfsg-2ubuntu1 amd64 Multiprecision arithmetic library (C++ bindings)

Z użyciem locate: Installation of MPFR previous versions of GMP clash

sudo updatedb locate libgmp

przykładowy wynik: /home/a/.tor-browser-en/INSTALL/Tor/libgmp.so /home/a/.tor-browser-en/INSTALL/Tor/libgmp.so.10 /home/a/.tor-browser-en/INSTALL/Tor/libgmp.so.10.1.3 /home/a/.tor-browser-en/INSTALL/Tor/libgmpxx.so /home/a/.tor-browser-en/INSTALL/Tor/libgmpxx.so.4 /home/a/.tor-browser-en/INSTALL/Tor/libgmpxx.so.4.3.3 /usr/lib/i386-linux-gnu/libgmp.so.10 /usr/lib/i386-linux-gnu/libgmp.so.10.1.2 /usr/lib/x86_64-linux-gnu/libgmp.so.10 /usr/lib/x86_64-linux-gnu/libgmp.so.10.1.2 /usr/lib/x86_64-linux-gnu/openssl-1.0.0/engines/libgmp.so /usr/share/doc/libgmp10 /usr/share/doc/libgmp10/README.Debian /usr/share/doc/libgmp10/changelog.Debian.gz /usr/share/doc/libgmp10/copyright /var/lib/dpkg/info/libgmp10:amd64.list /var/lib/dpkg/info/libgmp10:amd64.md5sums /var/lib/dpkg/info/libgmp10:amd64.postinst /var/lib/dpkg/info/libgmp10:amd64.postrm /var/lib/dpkg/info/libgmp10:amd64.shlibs /var/lib/dpkg/info/libgmp10:i386.list /var/lib/dpkg/info/libgmp10:i386.md5sums /var/lib/dpkg/info/libgmp10:i386.postinst /var/lib/dpkg/info/libgmp10:i386.postrm /var/lib/dpkg/info/libgmp10:i386.shlibs

Ubuntu Biblioteka GMP po instalacji ze źródeł będzie zainstalowana w kataloguehow : how_to_install-gmp-ubunt:

/usr/local/lib

Starsza wersja biblioteki pozostanie w:

/usr/lib/i386-linux-gnu/ # on 32-bit systems /usr/lib/x86_64-linux-gnu/ # on 64-bit systems

Jeśli twój program potrzebuje nowszej wersji to musisz go skonfigurować tak, aby szukał w katalogu:

/usr/local/lib

inaczej będzie korzystała ze starej wersji GMP

Typy danych

GMP używa własnych typów danych dla liczb wielokrotnej precyzji:Nomenclature and Types mpz_t dla liczb całkowitych mpq_t dla liczby wymiernych (C nie ma takiego typu) mpf_t dla liczb zmiennoprzecinkowych mp_exp_t dla wykładników (ang. exponent) (odpowiada long int) mp_limb_t ("A limb means the part of a multi-precision number that fits in a single word. (We chose this word because a limb of the human body is analogous to a digit, only larger, and containing several digits.) Normally a limb contains 32 or 64 bits.")GMP 3.1.1 Basics

Pozostałe typy: mp_size_t gmp_randstate_t size_t mp_bitcnt_t = typ dla oznaczenia precyzji liczby zmiennoprzecinkowej. ("Counts of bits of a multi-precision number are represented in the C type mp_bitcnt_t. Currently this is always an unsigned long, but on some systems it will be an unsigned long long in the future.")

Funkcje

Funkcje dla danego typu mają nazwę o tym samym przedrostku, np: mpz_init dla typu mpz_t mpq_init dla typu mpq_t mpf_init dla typu mpf_t

Możemy: inicjować kilka zmiennych tego samego typu jednocześnie: mpf_inits(sq_me, sq_out, test, NULL); inicjować i jednocześnie nadać wartość jednej zmiennej: mpz_init_set_ui usunąć kilka zmiennych jednocześnie: mpf_clears(sq_me, sq_out, test, NULL);

Używamy listy zmiennych zakończonej NULL.

Schemat programu deklaracja zmiennej inicjacja zmiennej (ang. init) przypisanie wartości (ang. set) przetwarzanie zmiennej usunięcie zmiennej (ang. clear)

/* skeleton of the gmp code by Sriram Sankaranarayanan http://www.cs.colorado.edu/~srirams/classes/doku.php/gmp_usage_tutorial

gcc g.c -lgmp -Wall ./a.out

*/ #include

int main(void){ mpz_t n; mpz_init(n); mpz_set_ui(n,0); /* tu wstaw kod przetwarzający zmienną n */ mpz_clear(n);

gmp_printf (" GMP-%s \n ", gmp_version );

return 0; }

init

mpz_set_str(z, "33877456965431938318210482471113262183356704085033125021829876006886584214655562", 10 ); // jeśli z > ui_max

mpz_set_ui(z, 6886584214655562); // jeśli z < ui_max

mpz_set_str (sq_me, argv, 10); // wczytywanie z listy argumentów

mpf_init2(avg, prec),

Powinno się sprawdzać poprawność inicjalizacji, przykład:

// mpfr_set_str function returns 0 if the entire string up to the final null character is a valid number in base base; otherwise it returns -1 set_result = mpfr_set_str(v, xs, 0, GMP_RNDN); if ( set_result < 0) { // error printf( "view line not valid, check chars \n"); // free memory because return without free is causing a memory leak free(xs);

Przykłady lista przykładów on GMP By Sriram Sankaranarayanan GNU GMP examples by ElieDeBrauwerC implementation of arithmetic with arbitrarily large integers by Vasek Chvatal pakiet libgmp10-doc zawiera przykładowe programy, które można znaleźć w katalogu: /usr/share/doc/libgmp10-doc/examples/demos/packages ubuntu trusty : libgmp10-doc filelist

Liczby całkowite

Mnożenie // gcc m.c -lgmp // ./a.out

#include #include #include

int main(void) { mpz_t x; mpz_t y; mpz_t result;

mpz_init(x); mpz_init(y); mpz_init(result);

mpz_set_str(x, "7612058254738945", 10); mpz_set_str(y, "9263591128439081", 10);

mpz_mul(result, x, y); gmp_printf("\n" " %Zd\n" "*\n" " %Zd\n" "--------------------\n" "%Zd\n" "\n", x, y, result);

/* free used memory */ mpz_clear(x); mpz_clear(y); mpz_clear(result); return EXIT_SUCCESS; }

Wynik:

7612058254738945 * 9263591128439081

70514995317761165008628990709545

Silnia Silnia (ang. factorial) Silnia w wikipedii // http://www.mersenneforum.org/showthread.php?t=3970 // C programme which will compute factorials

#include #include

int main (int argc, char **argv) { mpz_t x;

mpz_init (x); mpz_fac_ui (x, atoi (argv)); gmp_printf ("%Zd\n", x); mpz_clear (x);

return 0; }

Zapisujemy jako f.c i kompilujemy:

gcc f.c -lgmp

Uruchamiamy:

./a.out 10

Otrzymujemy wynik:

3628800

Bardziej skomplikowany przykład:

./a.out 100 93326215443944152681699238856266700490715968264381621468592963895217599993229915608941463976156518286253697920827223758251185210916864000000000000000000000000

Tablica

/*

http://stackoverflow.com/questions/26908628/gmp-mpz-array-init-is-an-obsolete-function-how-should-we-initialize-mpz-arrays

gcc a.c -lgmp

*/ #include // malloc #include #include

int main ()

int array_size = 100; int i; mpz_t *array; mpz_t temp;

// allocate memory for the array array = malloc(array_size * sizeof(mpz_t)); if (NULL == array) { printf("ERROR: Out of memory\n"); return 1;

// init mpz_init(temp); for (i = 0; i < array_size; i++) { mpz_init2(array[i], 1024);

// compute // using string and temp variable mpz_set_str (temp, "20023232323232323234343434", 10); mpz_set(array, temp); // using ui mpz_set_ui(array, 1212121);

// check gmp_printf ("%Zd\n",array); // gmp_printf ("%Zd\n", temp); // gmp_printf ("%Zd\n",array); //

// free memory for (i = 0; i < array_size; i++) { mpz_clear(array[i]);

free(array); mpz_clear(temp);

return 0; }

Liczby wymierne (dziesiętne i binarne)

Pierwszy przykład /*

C programme using gmp

gcc r.c -lgmp

http://gmplib.org/manual/Rational-Number-Functions.html#Rational-Number-Functions

*/

#include #include

// input = num/den unsigned int num = 1; unsigned int den = 6;

int main ()

mpq_t q; // rational number; int b =2 ; // base of numeral system mpz_t n ; mpz_t d ; mpf_t f ; char *sr; char *sf; mp_exp_t exponent ; // holds the exponent for the result string

// init and set variables mpq_init (q); // Initialize r and set it to 0/1. mpf_init (f); mpz_init_set_ui(n,num); mpz_init_set_ui(d,den); mpq_set_num(q,n); mpq_set_den(q,d); mpq_canonicalize (q); // It is the responsibility of the user to canonicalize the assigned variable before any arithmetic operations are performed on that variable. mpf_set_q(f,q);

// convertions sr = mpq_get_str (NULL, b, q); // rational number = fraction : from decimal to binary // If str is NULL, the result string is allocated using the current allocation function sf = mpf_get_str (NULL, &exponent, b, 0, f); // floating point number : from decimal to binary // If n_digits is 0 then that accurate maximum number of digits are generated.

// print gmp_printf ("a rational number %Zd / %Zd is in a canonical form ( decimal fraction) : %Qd\n",n,d, q); // gmp_printf(" numerator of rational number = %Zd \n", mpq_numref(q)); // gmp_printf(" denominator of rational number = %Zd \n", mpq_denref(q)); // gmp_printf (" binary rational ( string ) : %s \n", sr); // gmp_printf (" decimal floating point number : %.Ff \n", f); //

// The output of the GMP programme is in the form mantissa,'e',exponent where the value is // 0.mantissa * 2^exponent // GMP represents the floating point numbers (for base 2) as a pair of exponent and mantissa (and sign).

//The generated string is a fraction, with an implicit radix point immediately to the left of the first digit. // Trailing zeros are not returned. // For example, the number 3.1416 would be returned as string "31416" and exponent 1 so : // 3.1416 = 0.31416*10^1 // another example : 1/6 as a exponential form of binary number will be : // mantissa = 101010101010101010101010101010101010101010101010101010101010101011 // exponet = -2 // so 1/6 = 0.101010101010101010101010101010101010101010101010101010101010101011 * 2 ^(-2) = 0.0(01) gmp_printf (" mantissa of binary floating ( string ) : %s \n", sf); // gmp_printf (" exponent : %ld \n", exponent); // printf ("binary floating number in exponential form =0.%s*%d^%ld\n", sf,b, exponent);

// clear memory mpq_clear (q); mpz_clear (n); mpz_clear (d); mpf_clear (f);

return 0; }

Kompilujemy: gcc r.c -lgmp

Uruchamiamy ./a.out

Otrzymujemy:

a rational number 1 / 6 is in a canonical form ( decimal fraction) : 1/6 numerator of rational number = 1 denominator of rational number = 6 binary rational ( string ) : 1/110 decimal floating point number : 0.166666666666666666667