Next: , Previous: , Up: MPFR Interface   [Index]


5.2 Assignment Functions

These functions assign new values to already initialized floats (see Initialization Functions).

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

Set the value of rop from op, rounded toward the given direction rnd. Note that the input 0 is converted to +0 by mpfr_set_ui, mpfr_set_si, mpfr_set_uj, mpfr_set_sj, mpfr_set_z, mpfr_set_q and mpfr_set_f, regardless of the rounding mode. If the system does not support the IEEE 754 standard, mpfr_set_flt, mpfr_set_d, mpfr_set_ld and mpfr_set_decimal64 might not preserve the signed zeros. The mpfr_set_decimal64 function is built only with the configure option ‘--enable-decimal-float’, which also requires ‘--with-gmp-build’, and when the compiler or system provides the ‘_Decimal64’ data type (recent versions of GCC support this data type); to use mpfr_set_decimal64, one should define the macro MPFR_WANT_DECIMAL_FLOATS before including mpfr.h. mpfr_set_q might fail if the numerator (or the denominator) can not be represented 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_flt, mpfr_set_d, mpfr_set_ld or mpfr_set_decimal64. Otherwise the floating-point constant will be first converted into a reduced-precision (e.g., 53-bit) binary (or decimal, for mpfr_set_decimal64) number before MPFR can work with it.

Function: int mpfr_set_ui_2exp (mpfr_t rop, unsigned long int op, mpfr_exp_t e, mpfr_rnd_t rnd)
Function: int mpfr_set_si_2exp (mpfr_t rop, long int op, mpfr_exp_t e, mpfr_rnd_t rnd)
Function: int mpfr_set_uj_2exp (mpfr_t rop, uintmax_t op, intmax_t e, mpfr_rnd_t rnd)
Function: int mpfr_set_sj_2exp (mpfr_t rop, intmax_t op, intmax_t e, mpfr_rnd_t rnd)
Function: int mpfr_set_z_2exp (mpfr_t rop, mpz_t op, mpfr_exp_t e, mpfr_rnd_t rnd)

Set the value of rop from op multiplied by two to the power e, rounded toward 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, mpfr_rnd_t rnd)

Set rop to the value of the 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. Contrary to mpfr_strtofr, mpfr_set_str requires the whole string to represent a valid floating-point number.

The meaning of the return value differs from other MPFR functions: it is 0 if the entire string up to the final null character is a valid number in base base; otherwise it is −1, and rop may have changed (users interested in the ternary value should use mpfr_strtofr instead).

Note: it is preferable to use mpfr_strtofr if one wants to distinguish between an infinite rop value coming from an infinite s or from an overflow.

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

Read a floating-point number from a string nptr in base base, rounded in the direction rnd; base must be either 0 (to detect the base, as described below) or a number from 2 to 62 (otherwise the behavior is undefined). If nptr starts with valid data, the result is stored in rop and *endptr points to the character just after the valid data (if endptr is not a null pointer); otherwise rop is set to zero (for consistency with strtod) and the value of nptr is stored in the location referenced by endptr (if endptr is not a null pointer). The usual ternary value is returned.

Parsing follows the standard C strtod function with some extensions. After optional leading whitespace, one has a subject sequence consisting of an optional sign (+ or -), and either numeric data or special data. The subject sequence is defined as the longest initial subsequence of the input string, starting with the first non-whitespace character, that is of the expected form.

The form of numeric data is a non-empty sequence of significand digits with an optional decimal point, and an optional exponent consisting of an exponent prefix followed by an optional sign and a non-empty sequence of decimal digits. A significand digit is either a decimal digit or a Latin letter (62 possible characters), with A = 10, B = 11, …, Z = 35; case is ignored in bases less or equal to 36, in bases larger than 36, a = 36, b = 37, …, z = 61. The value of a significand digit must be strictly less than the base. 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). The exponent prefix can be e or E for bases up to 10, or @ in any base; it indicates a multiplication by a power of the base. In bases 2 and 16, the exponent prefix can also be p or P, in which case the exponent, called binary exponent, indicates a multiplication by a power of 2 instead of the base (there is a difference only for base 16); in base 16 for example 1p2 represents 4 whereas 1@2 represents 256. The value of an exponent is always written in base 10.

If the argument base is 0, then the base is automatically detected as follows. If the significand starts with 0b or 0B, base 2 is assumed. If the significand starts with 0x or 0X, base 16 is assumed. Otherwise base 10 is assumed.

Note: The exponent (if present) must contain at least a digit. Otherwise the possible exponent prefix and sign are not part of the number (which ends with the significand). Similarly, if 0b, 0B, 0x or 0X is not followed by a binary/hexadecimal digit, then the subject sequence stops at the character 0, thus 0 is read.

Special data (for infinities and NaN) can be @inf@ or @nan@(n-char-sequence-opt), and if base <= 16, it can also be infinity, inf, nan or nan(n-char-sequence-opt), all case insensitive. A n-char-sequence-opt is a possibly empty string containing only digits, Latin letters and the underscore (0, 1, 2, …, 9, a, b, …, z, A, B, …, Z, _). Note: one has an optional sign for all data, even NaN. For example, -@nAn@(This_Is_Not_17) is a valid representation for NaN in base 17.

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

Set the variable x to NaN (Not-a-Number), infinity or zero respectively. In mpfr_set_inf or mpfr_set_zero, x is set to plus infinity or plus zero iff sign is nonnegative; in mpfr_set_nan, the sign bit of the result is unspecified.

Function: void mpfr_swap (mpfr_t x, mpfr_t y)

Swap the structures pointed to by x and y. In particular, the values are exchanged without rounding (this may be different from three mpfr_set calls using a third auxiliary variable).

Warning! Since the precisions are exchanged, this will affect future assignments. Moreover, since the significand pointers are also exchanged, you must not use this function if the allocation method used for x and/or y does not permit it. This is the case when x and/or y were declared and initialized with MPFR_DECL_INIT, and possibly with mpfr_custom_init_set (see Custom Interface).


Next: , Previous: , Up: MPFR Interface   [Index]