Frequently Asked Questions about GNU 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.

  1. What are the differences between MPF from GMP and MPFR?
  2. How to convert my program written using MPF to MPFR?
  3. At configure time, I get the error: libgmp not found or uses a different ABI.
  4. I get undefined reference to __gmp_get_memory_functions.
  5. When I link my program with MPFR, I get undefined reference to __gmpXXXX.
  6. My program crashes with high precisions.
  7. Though I have increased the precision, the results are not more accurate.
  8. How can I detect MPFR installation using autoconf or pkg-config?
  9. How to cite MPFR in a scientific publication?
  10. When I build MPFR, I get an error asking me to recompile with -fPIC.
1. What are the differences between MPF from GMP and MPFR?

The main differences are:

2. 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 (MPFR_RNDN for rounding to nearest, MPFR_RNDZ for rounding toward zero, MPFR_RNDU for rounding toward plus infinity, MPFR_RNDD for rounding toward minus infinity). You can also define macros as follows: #define mpf_add(a, b, c) mpfr_add(a, b, c, MPFR_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.

3. At configure time, I get the error: libgmp not found or uses a different ABI.

This test (checking for __gmpz_init in -lgmp) comes after the gmp.h detection. The failure occurs either because the GMP library could not be found (as it is not in the provided library search paths) or because the GMP library that was found does not have the expected ABI (e.g. 32-bit vs 64-bit). The former problem can be due to the fact that a static build of MPFR was requested while only a shared GMP library is installed (or the opposite, but another error can also show up in this case, see the question about -fPIC). The latter problem can have several causes:

Note: The config.log output gives more information than the error message. In particular, see the output of the test: checking for CC and CFLAGS in gmp.h; it should give you the default compiler options (from gmp.h).

See also the answer to the next question.

4. I get undefined reference to __gmp_get_memory_functions.

Note: this was mainly a problem when upgrading from GMP 4.1.4 to a later version, but information given below may still be useful in other cases, when several GMP libraries are installed on the same machine.

If you get such an error, in particular when running make check, then this probably means that you are using the header file from GMP 4.2.x but the GMP 4.1.4 library. This can happen if several GMP versions are installed on your machine (e.g., one provided by the system in /usr/{include,lib} and a new one installed by the owner or administrator of the machine in /usr/local/{include,lib}) and your include and library search paths are inconsistent. On various GNU/Linux machines, this is unfortunately the case by default (/usr/local/include is in the default include search path, but /usr/local/lib is not in the default library search path). Typical errors are: undefined reference to `__gmp_get_memory_functions' in make check. The best solution is to add /usr/local/include to your C_INCLUDE_PATH environment variable and to add /usr/local/lib to your LIBRARY_PATH and LD_LIBRARY_PATH environment variables (and/or LD_RUN_PATH). Alternatively, you can use --with-gmp* configure options, e.g. --with-gmp=/usr/local, but this is not guaranteed to work (in particular with gcc and system directories such as /usr or /usr/local), and other software that uses GMP and/or MPFR will need correct paths too; environment variables allow you to set them in a global way.

Other information can be given in the INSTALL file and ld manual. Please look at them for more details. See also the next question.

5. 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.

If you used correct link options, but still get an error, this may mean that your include and library search paths are inconsistent. Please see the previous question.

6. 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:

7. 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, MPFR_RNDN) where the variable y is more precise than the variable x will not make it more accurate; the (binary) value will remain unchanged.

8. 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. Limited pkg-config support has been added for MPFR 4.0.0; example:

cc myprogram.c $(pkg-config --cflags --libs mpfr)
9. How to cite MPFR in a scientific publication?

To properly cite MPFR in a scientific publication, please cite the ACM TOMS paper and/or the library web page http://www.mpfr.org. If your publication is related to a particular release of MPFR, for example if you report timings, please also indicate the release number for future reference.

10. When I build MPFR, I get an error asking me to recompile with -fPIC.

A typical error looks like:

/usr/bin/ld: /path/to/libgmp.a(realloc.o): relocation R_X86_64_32 against `.rodata.str1.1' can not be used when making a shared object; recompile with -fPIC
/path/to/libgmp.a: could not read symbols: Bad value
collect2: ld returned 1 exit status

The probable reason is that you tried to build MPFR with the shared library enabled (this is the default), while only a static GMP library could be found. To solve this problem, either rebuild and reinstall GMP without the --disable-shared configure option, or configure MPFR with --disable-shared. If you did this and still get the above error, the cause may be conflicting GMP versions installed on your system; please check that your search path settings are correct.

Additional note about the last sentence: Under GNU/Linux (for instance), the linker takes the first library found in the library search path, whether it is dynamic or static. The default behavior under darwin is different, but MPFR will change it.