[ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
5.2 Assignment Functions
These functions assign new values to already initialized floats
(see section 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_decimal64 (mpfr_t rop, _Decimal64 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 toward 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
andmpfr_set_f
, regardless of the rounding mode. If the system doesn't support the IEEE-754 standard,mpfr_set_d
,mpfr_set_ld
andmpfr_set_decimal64
might not preserve the signed zeros. Thempfr_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 (GCC version 4.2.0 is known to support this data type, but only when configured with ‘--enable-decimal-float’ too).mpfr_set_q
might not be able to work if the numerator (or the denominator) can not be representable as ampfr_t
.Note: If you want to store a floating-point constant to a
mpfr_t
, you should usempfr_set_str
(or one of the MPFR constant functions, such asmpfr_const_pi
for Pi) instead ofmpfr_set_d
,mpfr_set_ld
ormpfr_set_decimal64
. 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 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, 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; base must be either 0 (to detect the base, as described below) or a number from 2 to 36 (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 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. Case is ignored. 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
= 36; its value 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 bee
orE
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 bep
orP
, in which case it introduces a binary exponent: it indicates a multiplication by a power of 2 (there is a difference only for base 16). The value of an exponent is always written in base 10. In base 2, the significand can start with0b
or0B
, and in base 16, it can start with0x
or0X
.If the argument base is 0, then the base is automatically detected as follows. If the significand starts with
0b
or0B
, base 2 is assumed. If the significand starts with0x
or0X
, base 16 is assumed. Otherwise base 10 is assumed.Note: The exponent 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
or0X
is not followed by a binary/hexadecimal digit, then the subject sequence stops at the character0
.Special data (for infinities and NaN) can be
@inf@
or@nan@(n-char-sequence)
, and if base <= 16, it can also beinfinity
,inf
,nan
ornan(n-char-sequence)
, all case insensitive. An-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, _). Note: one has an optional sign for all data, even NaN.
- 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 threempfr_set
calls using a third auxiliary variable.
[ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |