Next: Combined Initialization and Assignment Functions, Previous: Initialization Functions, Up: MPFR Interface
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.
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
andmpfr_set_f
, regardless of the rounding mode. If the system doesn't support the IEEE-754 standard,mpfr_set_d
andmpfr_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 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
ormpfr_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.
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.
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.
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 ane
orE
(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 leading0x
or0X
, in which casep
orP
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 leading0b
or0B
, in which casee
,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, ornan
,nan(n-char-sequence)
(if base <= 10),@nan@
or@nan@(n-char-sequence)
all case insensitive (as Latin letters), can be given. 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, _).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
or0B
is not followed by hexadecimal/binary digits, parsing stops after the first0
: 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 the0x
prefix, it assumes that base is 16. If it begins with0b
, 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.