Conversion Functions (GNU MPFR 4.1.0)
Description: How to install and use GNU MPFR, a library for reliable multiple precision floating-point arithmetic, version 4.1.0.

Next: Arithmetic Functions, Previous: Combined Initialization and Assignment Functions, Up: MPFR Interface [Index]

### 5.4 Conversion Functions

Function: float mpfr_get_flt (mpfr_t op, mpfr_rnd_t rnd)
Function: double mpfr_get_d (mpfr_t op, mpfr_rnd_t rnd)
Function: long double mpfr_get_ld (mpfr_t op, mpfr_rnd_t rnd)
Function: _Float128 mpfr_get_float128 (mpfr_t op, mpfr_rnd_t rnd)
Function: _Decimal64 mpfr_get_decimal64 (mpfr_t op, mpfr_rnd_t rnd)
Function: _Decimal128 mpfr_get_decimal128 (mpfr_t op, mpfr_rnd_t rnd)

  Convert op to a `float` (respectively `double`, `long double`, `_Decimal64`, or `_Decimal128`) using the rounding mode rnd. If op is NaN, some fixed NaN (either quiet or signaling) or the result of 0.0/0.0 is returned. If op is ±Inf, an infinity of the same sign or the result of ±1.0/0.0 is returned. If op is zero, these functions return a zero, trying to preserve its sign, if possible. The `mpfr_get_float128`, `mpfr_get_decimal64` and `mpfr_get_decimal128` functions are built only under some conditions: see the documentation of `mpfr_set_float128`, `mpfr_set_decimal64` and `mpfr_set_decimal128` respectively.

Function: long mpfr_get_si (mpfr_t op, mpfr_rnd_t rnd)
Function: unsigned long mpfr_get_ui (mpfr_t op, mpfr_rnd_t rnd)
Function: intmax_t mpfr_get_sj (mpfr_t op, mpfr_rnd_t rnd)
Function: uintmax_t mpfr_get_uj (mpfr_t op, mpfr_rnd_t rnd)

  Convert op to a `long`, an `unsigned long`, an `intmax_t` or an `uintmax_t` (respectively) after rounding it to an integer with respect to rnd. If op is NaN, 0 is returned and the erange flag is set. If op is too big for the return type, the function returns the maximum or the minimum of the corresponding C type, depending on the direction of the overflow; the erange flag is set too. When there is no such range error, if the return value differs from op, i.e., if op is not an integer, the inexact flag is set. See also `mpfr_fits_slong_p`, `mpfr_fits_ulong_p`, `mpfr_fits_intmax_p` and `mpfr_fits_uintmax_p`.

Function: double mpfr_get_d_2exp (long *exp, mpfr_t op, mpfr_rnd_t rnd)
Function: long double mpfr_get_ld_2exp (long *exp, mpfr_t op, mpfr_rnd_t rnd)

  Return d and set exp (formally, the value pointed to by exp) such that 0.5<=abs(d)<1 and d times 2 raised to exp equals op rounded to double (resp. long double) precision, using the given rounding mode. If op is zero, then a zero of the same sign (or an unsigned zero, if the implementation does not have signed zeros) is returned, and exp is set to 0. If op is NaN or an infinity, then the corresponding double precision (resp. long-double precision) value is returned, and exp is undefined.

Function: int mpfr_frexp (mpfr_exp_t *exp, mpfr_t y, mpfr_t x, mpfr_rnd_t rnd)

  Set exp (formally, the value pointed to by exp) and y such that 0.5<=abs(y)<1 and y times 2 raised to exp equals x rounded to the precision of y, using the given rounding mode. If x is zero, then y is set to a zero of the same sign and exp is set to 0. If x is NaN or an infinity, then y is set to the same value and exp is undefined.

Function: mpfr_exp_t mpfr_get_z_2exp (mpz_t rop, mpfr_t op)

  Put the scaled significand of op (regarded as an integer, with the precision of op) into rop, and return the exponent exp (which may be outside the current exponent range) such that op exactly equals rop times 2 raised to the power exp. If op is zero, the minimal exponent `emin` is returned. If op is NaN or an infinity, the erange flag is set, rop is set to 0, and the the minimal exponent `emin` is returned. The returned exponent may be less than the minimal exponent `emin` of MPFR numbers in the current exponent range; in case the exponent is not representable in the `mpfr_exp_t` type, the erange flag is set and the minimal value of the `mpfr_exp_t` type is returned.

Function: int mpfr_get_z (mpz_t rop, mpfr_t op, mpfr_rnd_t rnd)

  Convert op to a `mpz_t`, after rounding it with respect to rnd. If op is NaN or an infinity, the erange flag is set, rop is set to 0, and 0 is returned. Otherwise the return value is zero when rop is equal to op (i.e., when op is an integer), positive when it is greater than op, and negative when it is smaller than op; moreover, if rop differs from op, i.e., if op is not an integer, the inexact flag is set.

Function: void mpfr_get_q (mpq_t rop, mpfr_t op)

  Convert op to a `mpq_t`. If op is NaN or an infinity, the erange flag is set and rop is set to 0. Otherwise the conversion is always exact.

Function: int mpfr_get_f (mpf_t rop, mpfr_t op, mpfr_rnd_t rnd)

  Convert op to a `mpf_t`, after rounding it with respect to rnd. The erange flag is set if op is NaN or an infinity, which do not exist in MPF. If op is NaN, then rop is undefined. If op is +Inf (resp. −Inf), then rop is set to the maximum (resp. minimum) value in the precision of the MPF number; if a future MPF version supports infinities, this behavior will be considered incorrect and will change (portable programs should assume that rop is set either to this finite number or to an infinite number). Note that since MPFR currently has the same exponent type as MPF (but not with the same radix), the range of values is much larger in MPF than in MPFR, so that an overflow or underflow is not possible.

Function: size_t mpfr_get_str_ndigits (int b, mpfr_prec_t p)

  Return the minimal integer m such that any number of p bits, when output with m digits in radix b with rounding to nearest, can be recovered exactly when read again, still with rounding to nearest. More precisely, we have m = 1 + ceil(p*log(2)/log(b)), with p replaced by p−1 if b is a power of 2.

  The argument b must be in the range 2 to 62; this is the range of bases supported by the `mpfr_get_str` function. Note that contrary to the base argument of this function, negative values are not accepted.

Function: char * mpfr_get_str (char *str, mpfr_exp_t *expptr, int base, size_t n, mpfr_t op, mpfr_rnd_t rnd)

  Convert op to a string of digits in base abs(base), with rounding in the direction rnd, where n is either zero (see below) or the number of significant digits output in the string. The argument base may vary from 2 to 62 or from −2 to −36; otherwise the function does nothing and immediately returns a null pointer.

  For base in the range 2 to 36, digits and lower-case letters are used; for −2 to −36, digits and upper-case letters are used; for 37 to 62, digits, upper-case letters, and lower-case letters, in that significance order, are used. Warning! This implies that for base > 10, the successor of the digit 9 depends on base. This choice has been done for compatibility with GMP’s `mpf_get_str` function. Users who wish a more consistent behavior should write a simple wrapper.

  If the input is NaN, then the returned string is ‘`@NaN@`’ and the NaN flag is set. If the input is +Inf (resp. −Inf), then the returned string is ‘`@Inf@`’ (resp. ‘`-@Inf@`’).

  If the input number is a finite number, the exponent is written through the pointer expptr (for input 0, the current minimal exponent is written); the type `mpfr_exp_t` is large enough to hold the exponent in all cases.

  The generated string is a fraction, with an implicit radix point immediately to the left of the first digit. For example, the number −3.1416 would be returned as "−31416" in the string and 1 written at expptr. If rnd is to nearest, and op is exactly in the middle of two consecutive possible outputs, the one with an even significand is chosen, where both significands are considered with the exponent of op. Note that for an odd base, this may not correspond to an even last digit: for example with 2 digits in base 7, (14) and a half is rounded to (15) which is 12 in decimal, (16) and a half is rounded to (20) which is 14 in decimal, and (26) and a half is rounded to (26) which is 20 in decimal.

  If n is zero, the number of digits of the significand is taken as `mpfr_get_str_ndigits(base,p)` where p is the precision of op (see mpfr_get_str_ndigits).

  If str is a null pointer, space for the significand is allocated using the allocation function (see Memory Handling) and a pointer to the string is returned (unless the base is invalid). To free the returned string, you must use `mpfr_free_str`.

  If str is not a null pointer, it should point to a block of storage large enough for the significand. A safe block size (sufficient for any value) is `max(n + 2, 7)` if n is not zero; if n is zero, replace it by `mpfr_get_str_ndigits(base,p)` where p is the precision of op, as mentioned above. The extra two bytes are for a possible minus sign, and for the terminating null character, and the value 7 accounts for ‘`-@Inf@`’ plus the terminating null character. The pointer to the string str is returned (unless the base is invalid).

  Like in usual functions, the inexact flag is set iff the result is inexact.

Function: void mpfr_free_str (char *str)

  Free a string allocated by `mpfr_get_str` using the unallocation function (see Memory Handling). The block is assumed to be `strlen(str)+1` bytes.

Function: int mpfr_fits_ulong_p (mpfr_t op, mpfr_rnd_t rnd)
Function: int mpfr_fits_slong_p (mpfr_t op, mpfr_rnd_t rnd)
Function: int mpfr_fits_uint_p (mpfr_t op, mpfr_rnd_t rnd)
Function: int mpfr_fits_sint_p (mpfr_t op, mpfr_rnd_t rnd)
Function: int mpfr_fits_ushort_p (mpfr_t op, mpfr_rnd_t rnd)
Function: int mpfr_fits_sshort_p (mpfr_t op, mpfr_rnd_t rnd)
Function: int mpfr_fits_uintmax_p (mpfr_t op, mpfr_rnd_t rnd)
Function: int mpfr_fits_intmax_p (mpfr_t op, mpfr_rnd_t rnd)

  Return non-zero if op would fit in the respective C data type, respectively `unsigned long`, `long`, `unsigned int`, `int`, `unsigned short`, `short`, `uintmax_t`, `intmax_t`, when rounded to an integer in the direction rnd. For instance, with the `MPFR_RNDU` rounding mode on −0.5, the result will be non-zero for all these functions. For `MPFR_RNDF`, those functions return non-zero when it is guaranteed that the corresponding conversion function (for example `mpfr_get_ui` for `mpfr_fits_ulong_p`), when called with faithful rounding, will always return a number that is representable in the corresponding type. As a consequence, for `MPFR_RNDF`, `mpfr_fits_ulong_p` will return non-zero for a non-negative number less than or equal to `ULONG_MAX`.

Next: Arithmetic Functions, Previous: Combined Initialization and Assignment Functions, Up: MPFR Interface [Index]