An mpfr_t object must be initialized before storing the first value in
it. The functions mpfr_init and mpfr_init2 are used for that
purpose.
Initialize x, set its precision to be exactly prec bits and its value to NaN. (Warning: the corresponding
mpffunctions initialize to zero instead.)Normally, a variable should be initialized once only or at least be cleared, using
mpfr_clear, between initializations. To change the precision of a variable which has already been initialized, usempfr_set_prec. The precision prec must be an integer betweenMPFR_PREC_MINandMPFR_PREC_MAX(otherwise the behavior is undefined).
Initialize all the
mpfr_tvariables of the givenva_list, set their precision to be exactly prec bits and their value to NaN. Seempfr_init2for more details. Theva_listis assumed to be composed only of typempfr_t(or equivalentlympfr_ptr). It begins from x. It ends when it encounters a null pointer (whose type must also bempfr_ptr).
Free the space occupied by x. Make sure to call this function for all
mpfr_tvariables when you are done with them.
Free the space occupied by all the
mpfr_tvariables of the givenva_list. Seempfr_clearfor more details. Theva_listis assumed to be composed only of typempfr_t(or equivalentlympfr_ptr). It begins from x. It ends when it encounters a null pointer (whose type must also bempfr_ptr).
Here is an example of how to use multiple initialization functions:
{
mpfr_t x, y, z, t;
mpfr_inits2 (256, x, y, z, t, (mpfr_ptr) 0);
...
mpfr_clears (x, y, z, t, (mpfr_ptr) 0);
}
Initialize x and set its value to NaN.
Normally, a variable should be initialized once only or at least be cleared, using
mpfr_clear, between initializations. The precision of x is the default precision, which can be changed by a call tompfr_set_default_prec.Warning! In a given program, some other libraries might change the default precision and not restore it. Thus it is safer to use
mpfr_init2.
Initialize all the
mpfr_tvariables of the givenva_list, set their precision to be the default precision and their value to NaN. Seempfr_initfor more details. Theva_listis assumed to be composed only of typempfr_t(or equivalentlympfr_ptr). It begins from x. It ends when it encounters a null pointer (whose type must also bempfr_ptr).Warning! In a given program, some other libraries might change the default precision and not restore it. Thus it is safer to use
mpfr_inits2.
This macro declares name as an automatic variable of type
mpfr_t, initializes it and sets its precision to be exactly prec bits and its value to NaN. name must be a valid identifier. You must use this macro in the declaration section. This macro is much faster than usingmpfr_init2but has some drawbacks:
- You must not call
mpfr_clearwith variables created with this macro (the storage is allocated at the point of declaration and deallocated when the brace-level is exited).- You cannot change their precision.
- You should not create variables with huge precision with this macro.
- Your compiler must support ‘Non-Constant Initializers’ (standard in C++ and ISO C99) and ‘Token Pasting’ (standard in ISO C89). If prec is not a constant expression, your compiler must support ‘variable-length automatic arrays’ (standard in ISO C99). ‘GCC 2.95.3’ and above supports all these features. If you compile your program with gcc in c89 mode and with ‘-pedantic’, you may want to define the
MPFR_USE_EXTENSIONmacro to avoid warnings due to theMPFR_DECL_INITimplementation.
Set the default precision to be exactly prec bits. The precision of a variable means the number of bits used to store its significand. All subsequent calls to
mpfr_initwill use this precision, but previously initialized variables are unaffected. This default precision is set to 53 bits initially. The precision can be any integer betweenMPFR_PREC_MINandMPFR_PREC_MAX.
Here is an example on how to initialize floating-point variables:
{
mpfr_t x, y;
mpfr_init (x); /* use default precision */
mpfr_init2 (y, 256); /* precision exactly 256 bits */
...
/* When the program is about to exit, do ... */
mpfr_clear (x);
mpfr_clear (y);
mpfr_free_cache ();
}
The following functions are useful for changing the precision during a calculation. A typical use would be for adjusting the precision gradually in iterative algorithms like Newton-Raphson, making the computation precision closely match the actual accurate part of the numbers.
Reset the precision of x to be exactly prec bits, and set its value to NaN. The previous value stored in x is lost. It is equivalent to a call to
mpfr_clear(x)followed by a call tompfr_init2(x, prec), but more efficient as no allocation is done in case the current allocated space for the significand of x is enough. The precision prec can be any integer betweenMPFR_PREC_MINandMPFR_PREC_MAX.In case you want to keep the previous value stored in x, use
mpfr_prec_roundinstead.