Node:Converting Integers, Next:, Previous:Simultaneous Integer Init & Assign, Up:Integer Functions



Conversion Functions

This section describes functions for converting GMP integers to standard C types. Functions for converting to GMP integers are described in Assigning Integers and I/O of Integers.

unsigned long int mpz_get_ui (mpz_t op) Function
Return the value of op as an unsigned long.

If op is too big to fit an unsigned long then just the least significant bits that do fit are returned. The sign of op is ignored, only the absolute value is used.

signed long int mpz_get_si (mpz_t op) Function
If op fits into a signed long int return the value of op. Otherwise return the least significant part of op, with the same sign as op.

If op is too big to fit in a signed long int, the returned result is probably not very useful. To find out if the value will fit, use the function mpz_fits_slong_p.

double mpz_get_d (mpz_t op) Function
Convert op to a double.

double mpz_get_d_2exp (signed long int *exp, mpz_t op) Function
Find d and exp such that d times 2 raised to exp, with 0.5<=abs(d)<1, is a good approximation to op.

char * mpz_get_str (char *str, int base, mpz_t op) Function
Convert op to a string of digits in base base. The base may vary from 2 to 36.

If str is NULL, the result string is allocated using the current allocation function (see Custom Allocation). The block will be strlen(str)+1 bytes, that being exactly enough for the string and null-terminator.

If str is not NULL, it should point to a block of storage large enough for the result, that being mpz_sizeinbase (op, base) + 2. The two extra bytes are for a possible minus sign, and the null-terminator.

A pointer to the result string is returned, being either the allocated block, or the given str.

mp_limb_t mpz_getlimbn (mpz_t op, mp_size_t n) Function
Return limb number n from op. The sign of op is ignored, just the absolute value is used. The least significant limb is number 0.

mpz_size can be used to find how many limbs make up op. mpz_getlimbn returns zero if n is outside the range 0 to mpz_size(op)-1.