Next: , Previous: Initialization Functions, Up: MPFR Interface


5.2 Assignment Functions

These functions assign new values to already initialized floats (see Initialization Functions). When using any functions using intmax_t, you must include <stdint.h> or <inttypes.h> before mpfr.h, to allow mpfr.h to define prototypes for these functions.

— Function: int mpfr_set (mpfr_t rop, mpfr_t op, mp_rnd_t rnd)
— Function: int mpfr_set_ui (mpfr_t rop, unsigned long int op, mp_rnd_t rnd)
— Function: int mpfr_set_si (mpfr_t rop, long int op, mp_rnd_t rnd)
— Function: int mpfr_set_uj (mpfr_t rop, uintmax_t op, mp_rnd_t rnd)
— Function: int mpfr_set_sj (mpfr_t rop, intmax_t op, mp_rnd_t rnd)
— Function: int mpfr_set_d (mpfr_t rop, double op, mp_rnd_t rnd)
— Function: int mpfr_set_ld (mpfr_t rop, long double op, mp_rnd_t rnd)
— Function: int mpfr_set_z (mpfr_t rop, mpz_t op, mp_rnd_t rnd)
— Function: int mpfr_set_q (mpfr_t rop, mpq_t op, mp_rnd_t rnd)
— Function: int mpfr_set_f (mpfr_t rop, mpf_t op, mp_rnd_t rnd)

Set the value of rop from op, rounded towards the given direction rnd. Note that the input 0 is converted to +0 by mpfr_set_ui, mpfr_set_si, mpfr_set_sj, mpfr_set_uj, mpfr_set_z, mpfr_set_q and mpfr_set_f, regardless of the rounding mode. If the system doesn't support the IEEE-754 standard, mpfr_set_d and mpfr_set_ld might not preserve the signed zeros. mpfr_set_q might not be able to work if the numerator (or the denominator) can not be representable as a mpfr_t.

Note: If you want to store a floating-point constant to a mpfr_t, you should use mpfr_set_str (or one of the MPFR constant functions, such as mpfr_const_pi for Pi) instead of mpfr_set_d or mpfr_set_ld. Otherwise the floating-point constant will be first converted into a reduced-precision (e.g., 53-bit) binary number before MPFR can work with it.

— Function: int mpfr_set_ui_2exp (mpfr_t rop, unsigned long int op, mp_exp_t e, mp_rnd_t rnd)
— Function: int mpfr_set_si_2exp (mpfr_t rop, long int op, mp_exp_t e, mp_rnd_t rnd)
— Function: int mpfr_set_uj_2exp (mpfr_t rop, uintmax_t op, intmax_t e, mp_rnd_t rnd)
— Function: int mpfr_set_sj_2exp (mpfr_t rop, intmax_t op, intmax_t e, mp_rnd_t rnd)

Set the value of rop from op multiplied by two to the power e, rounded towards the given direction rnd. Note that the input 0 is converted to +0.

— Function: int mpfr_set_str (mpfr_t rop, const char *s, int base, mp_rnd_t rnd)

Set rop to the value of the whole string s in base base, rounded in the direction rnd. See the documentation of mpfr_strtofr for a detailed description of the valid string formats. This function returns 0 if the entire string up to the final null character is a valid number in base base; otherwise it returns −1, and rop may have changed.

— Function: int mpfr_strtofr (mpfr_t rop, const char *nptr, char **endptr, int base, mp_rnd_t rnd)

Read a floating-point number from a string nptr in base base, rounded in the direction rnd. If successful, the result is stored in rop and *endptr points to the character just after those parsed. If str doesn't start with a valid number then rop is set to zero and the value of nptr is stored in the location referenced by endptr.

Parsing follows the standard C strtod function. This means optional leading whitespace, an optional + or -, mantissa digits with an optional decimal point, and an optional exponent consisting of an e or E (if base <= 10) or @, an optional sign, and digits. The decimal point can be either the one defined by the current locale or the period (the first one is accepted for consistency with the C standard and the practice, the second one is accepted to allow the programmer to provide MPFR numbers from strings in a way that does not depend on the current locale). A hexadecimal mantissa can be given with a leading 0x or 0X, in which case p or P may introduce an optional binary exponent, indicating the power of 2 by which the mantissa is to be scaled. A binary mantissa can be given with a leading 0b or 0B, in which case e, E, p, P or @ may introduce the binary exponent. The exponent is always written in base 10.

In addition, infinity, inf (if base <= 10) or @inf@ with an optional sign, or nan, nan(n-char-sequence) (if base <= 10), @nan@ or @nan@(n-char-sequence) all case insensitive (as Latin letters), can be given. A n-char-sequence is a non-empty string containing only digits, Latin letters and the underscore (0, 1, 2, ..., 9, a, b, ..., z, A, B, ..., Z, _).

There must be at least one digit in the mantissa for the number to be valid. If an exponent has no digits it's ignored and parsing stops after the mantissa. If an 0x, 0X, 0b or 0B is not followed by hexadecimal/binary digits, parsing stops after the first 0: the subject sequence is defined as the longest initial subsequence of the input string, starting with the first non-white-space character, that is of the expected form. The subject sequence contains no characters if the input string is not of the expected form.

Note that in the hex format the exponent P represents a power of 2, whereas @ represents a power of the base (i.e. 16).

If the argument base is different from 0, it must be in the range 2 to 36. Case is ignored; uppercase and lowercase letters have the same value.

If base is 0, then it tries to identify the used base: if the mantissa begins with the 0x prefix, it assumes that base is 16. If it begins with 0b, it assumes that base is 2. Otherwise, it assumes it is 10.

It returns a usual ternary value. If endptr is not a null pointer, a pointer to the character after the last character used in the conversion is stored in the location referenced by endptr.

— Function: void mpfr_set_inf (mpfr_t x, int sign)
— Function: void mpfr_set_nan (mpfr_t x)

Set the variable x to infinity or NaN (Not-a-Number) respectively. In mpfr_set_inf, x is set to plus infinity iff sign is nonnegative.

— Function: void mpfr_swap (mpfr_t x, mpfr_t y)

Swap the values x and y efficiently. Warning: the precisions are exchanged too; in case the precisions are different, mpfr_swap is thus not equivalent to three mpfr_set calls using a third auxiliary variable.