Frequently Asked Questions about MPFR

Important notice: Problems with a particular version of MPFR are discussed in the corresponding bugs page.

The latest version of this FAQ is available at http://www.mpfr.org/faq.html. Please look at this version if possible.

What are the differences between MPF from GMP and MPFR?

The main differences are:

How to convert my program written using MPF to MPFR?

You need to add r to the function names, and to specify the rounding mode (GMP_RNDN for rounding to nearest, GMP_RNDZ for rounding towards zero, GMP_RNDU for rounding towards plus infinity, GMP_RNDD for rounding towards minus infinity). You can also define macros as follows: #define mpf_add(a, b, c) mpfr_add(a, b, c, GMP_RNDN)

The header file mpf2mpfr.h from the MPFR distribution automatically redefines all MPF functions in this way, using the default MPFR rounding mode. Thus you simply need to add the following line in all your files using MPF functions: #include <mpf2mpfr.h> just after the gmp.h and mpfr.h header files. If the program uses MPF internals (such as direct access to __mpf_struct members), additional changes will be needed.

When I link my program with MPFR, I get undefined reference to __gmpXXXX.

Link your program with GMP. Assuming that your program is foo.c, you should link it using: cc link.c -lmpfr -lgmp MPFR library reference (-lmpfr) should be before GMP's one (-lgmp). Another solution is, with GNU ld, to give all the libraries inside a group: gcc link.c -Wl,--start-group libgmp.a libmpfr.a -Wl,--end-group See INSTALL file and ld manual for more details.

Moreover, if several GMP versions are installed (e.g., one provided by the system and a new one installed by some user), you must make sure that the include and library search paths are consistent. Unfortunately, on various GNU/Linux machines, they aren't by default. Typical errors are: undefined reference to `__gmp_get_memory_functions' in make check when GMP 4.1.4 is installed in /usr/{include,lib} (provided by the system) and GMP 4.2.1 is installed in /usr/local/{include,lib} (installed by the user with configure, make, make install).

My program crashes with high precisions.

Your stack size limit may be too small; indeed, by default, GMP 4.1.4 and below allocates all temporary results on the stack, and in very high precisions, this limit may be reached. You can solve this problem in different ways:

Though I have increased the precision, the results are not more accurate.

The reason may be the use of C floating-point numbers. 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 π) 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. This is the case in particular for most exact decimal numbers, such as 0.17, which are not exactly representable in binary.

Also remember that MPFR does not track the accuracy of the results: copying a value x to y with mpfr_set (y, x, GMP_RNDN) where the variable y is more precise than the variable x will not make it more accurate; the (binary) value will remain unchanged.

How can I detect MPFR installation using autoconf or pkg-config?

The MPFR team does not currently recommend any autoconf code, but a section will later be added to the MPFR manual. The MPFR team does not wish to support pkg-config yet.