# 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:

  - The precision of a MPFR variable is the exact number of bits used for its mantissa, whereas in MPF, the precision requested by the user is a minimum value (MPF generally uses a higher precision). With the additional difference below, this implies that the MPFR results do not depend on the number of bits (16, 32, 64 or more) of the underlying architecture.

  - As a consequence, MPFR uses a base-2 exponent, whereas in MPF, this is a base-232 or base-264 exponent, depending on the limb size. For this reason (and other internal ones), the maximum exponent range in MPFR is different (and smaller, if the exponent is represented by the same type as in MPF).

  - MPFR provides an additional rounding mode argument to its functions; furthermore, it is guaranteed that the result of any operation is the nearest possible floating-point value from the exact result (considering the input variables as exact values), taking into account the precision of the destination variable and the rounding mode. MPFR also says whether the rounded result is above or below the exact result.

  - MPFR supports much more functions (in particular transcendental functions such as exponentials, logarithms, trigonometric functions and so on) and special values: signed zeros, infinities, not-a-number (NaN).

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 towards zero, `MPFR_RNDU` for rounding towards plus infinity, `MPFR_RNDD` for rounding towards 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:

  - A wrong libgmp library has been picked up. This can occur if you have several GMP versions installed on the machine and something is wrong with the provided library search paths.
  - Wrong compiler options (`CFLAGS`) were given. In general, the presence or absence of the `-m64` compiler option must match the library ABI.
  - A wrong `gmp.h` file has been picked up (if you have several GMP versions installed). Indeed, by default, MPFR gets the compiler options from the `gmp.h` file (with GMP 4.2.3 or later); this is needed because GMP does not necessarily use the default ABI. The consequence is that if the `gmp.h` file is associated with a library using a different ABI, the ABI-related options will be incorrect. Hence the failure.

  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:

  - You can upgrade to GMP 4.2 (or above), which now makes temporary allocations on the stack only when they are small.

  - You can increase the stack size limit with the `limit`, `unlimit` or `ulimit` command, depending on your shell. This may fail on some systems, where the maximum stack size cannot be increased above some value.

  - You can rebuild both GMP and MPFR to use another allocation method.

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. The MPFR team does not wish to support pkg-config yet.

9. How to cite MPFR in a scientific publication?

  To properly cite MPFR in a scientific publication, please cite the ACM TOMS paper (BibTeX) 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.