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

Next: Basic 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: _Decimal64 mpfr_get_decimal64 (mpfr_t op, mpfr_rnd_t rnd)

  Convert op to a `float` (respectively `double`, `long double` or `_Decimal64`), 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_decimal64` function is built only under some conditions: see the documentation of `mpfr_set_decimal64`.

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 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. 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.

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: char * mpfr_get_str (char *str, mpfr_exp_t *expptr, int b, size_t n, mpfr_t op, mpfr_rnd_t rnd)

  Convert op to a string of digits in base b, with rounding in the direction rnd, where n is either zero (see below) or the number of significant digits output in the string; in the latter case, n must be greater or equal to 2. The base may vary from 2 to 62; otherwise the function does nothing and immediately returns a null pointer. If the input number is an ordinary 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 chosen large enough so that re-reading the printed value with the same precision, assuming both output and input use rounding to nearest, will recover the original value of op. More precisely, in most cases, the chosen precision of str is the minimal precision m depending only on p = PREC(op) and b that satisfies the above property, i.e., m = 1 + ceil(p*log(2)/log(b)), with p replaced by p−1 if b is a power of 2, but in some very rare cases, it might be m+1 (the smallest case for bases up to 62 is when p equals 186564318007 for bases 7 and 49).

  If str is a null pointer, space for the significand is allocated using the current allocation function 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, i.e., at least `max(n + 2, 7)`. 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).

  Note: The NaN and inexact flags are currently not set when need be; this will be fixed in future versions. Programmers should currently assume that whether the flags are set by this function is unspecified.

Function: void mpfr_free_str (char *str)

  Free a string allocated by `mpfr_get_str` using the current unallocation function. The block is assumed to be `strlen(str)+1` bytes. For more information about how it is done: see Section “Custom Allocation” in GNU MP.

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.

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