5.4 Conversion Functions

This section describes functions for converting GMP integers to standard C types. Functions for converting to GMP integers are described in Assignment Functions and Input and Output Functions.

Function: unsigned long int mpz_get_ui (const mpz_t op)

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.

Function: signed long int mpz_get_si (const mpz_t op)

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.

Function: double mpz_get_d (const mpz_t op)

Convert op to a double, truncating if necessary (i.e. rounding towards zero).

If the exponent from the conversion is too big, the result is system dependent. An infinity is returned where available. A hardware overflow trap may or may not occur.

Function: double mpz_get_d_2exp (signed long int *exp, const mpz_t op)

Convert op to a double, truncating if necessary (i.e. rounding towards zero), and returning the exponent separately.

The return value is in the range 0.5<=abs(d)<1 and the exponent is stored to *exp. d * 2^exp is the (truncated) op value. If op is zero, the return is 0.0 and 0 is stored to *exp.

This is similar to the standard C frexp function (see Normalization Functions in The GNU C Library Reference Manual).

Function: char * mpz_get_str (char *str, int base, const mpz_t op)

Convert op to a string of digits in base base. The base argument may vary from 2 to 62 or from −2 to −36.

For base in the range 2..36, digits and lower-case letters are used; for −2..−36, digits and upper-case letters are used; for 37..62, digits, upper-case letters, and lower-case letters (in that significance order) are used.

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.