MPFR

The Multiple Precision Floating-Point Reliable Library Edition 1.0 June 1999

the PolKA project, INRIA Lorraine and LORIA

Copyright \begin{matrix}c \\ ◯ \end{matrix} 1999 The PolKA project

Published by the Free Software Foundation 59 Temple Place - Suite 330 Boston, MA 02111-1307, USA

Permission is granted to make and distribute verbatim copies of this manual provided the copyright notice and this permission notice are preserved on all copies.

Permission is granted to copy and distribute modified versions of this manual under the conditions for verbatim copying, provided that the entire resulting derived work is distributed under the terms of a permission notice identical to this one.

Permission is granted to copy and distribute translations of this manual into another language, under the above conditions for modified versions, except that this permission notice may be stated in a translation approved by the Foundation.

MPFR Copying Conditions

This library is free; this means that everyone is free to use it and free to redistribute it on a free basis. The library is not in the public domain; it is copyrighted and there are restrictions on its distribution, but these restrictions are designed to permit everything that a good cooperating citizen would want to do. What is not allowed is to try to prevent others from further sharing any version of this library that they might get from you.

Specifically, we want to make sure that you have the right to give away copies of the library, that you receive source code or else can get it if you want it, that you can change this library or use pieces of it in new free programs, and that you know you can do these things.

To make sure that everyone has such rights, we have to forbid you to deprive anyone else of these rights. For example, if you distribute copies of the MPFR library, you must give the recipients all the rights that you have. You must make sure that they, too, receive or can get the source code. And you must tell them their rights.

Also, for our own protection, we must make certain that everyone finds out that there is no warranty for the MPFR library. If it is modified by someone else and passed on, we want their recipients to know that what they have is not what we distributed, so that any problems introduced by others will not reflect on our reputation.

The precise conditions of the license for the MPFR library are found in the Library General Public License that accompany the source code. See the file COPYING.

1 Introduction to MPFR

MPFR is a portable library written in C for arbitrary precision arithmetic on reliable floatingpoint numbers. It is based on the GNU MP library. It aims to extend the class of floating-point numbers provided by the GNU MP library by reliable floating-point numbers. It may replace the GNU MP floating-point numbers in a future release. The main differences with the mpf class are:

• the mpfr code is portable, i.e. the result of any operation does not depend (or should not) on the machine word size mp_bits_per_limb (32 or 64 on most machines);

• the precision in bits can be set exactly to any positive value for each variable (including one bit of precision);

• mpfr provides the four rounding modes from the IEEE 754 standard.

In particular, with a precision of 53 bits, mpfr should be able to exactly reproduce all computations with double-precision machine floating-point numbers (double type in C), except the range of values is much wider (the exponent has at least 32 bits instead of 11).

This version of MPFR is released under the GNU Library General Public License. It is permitted to link MPFR to non-free programs, as long as MPFR source code is provided when distributing the non-free program.

1.1 How to use this Manual

Everyone should read Chapter 3 [MPFR Basics], page 5. If you need to install the library yourself, you need to read Chapter 2 [Installing MPFR], page 3, too.

The rest of the manual can be used for later reference, although it is probably a good idea to glance through it.

2 Installing MPFR

To build MPFR, you first have to install GNU MP (version 3.0 or higher) on your computer. You need a C compiler, preferably GCC, but any reasonable compiler should work. And you need a standard Unix ‘make’ program, plus some other standard Unix utility programs.

Here are the steps needed to install the library on Unix systems:

1. In most cases, ‘./configure --with-gmp=/usr/local/gmp’ should work, where ‘/usr/local/gmp’

is the directory where you have installed GNU MP. When you install GNU MP, you

have to copy the files ‘config.h’, ‘gmp-impl.h’, ‘gmp-mparam.h’, and ‘longlong.h’

from the GNU MP source directory to ‘/usr/local/gmp/include’; these additional files are needed by MPFR. An alternative way is ‘./configure --with-gmp-include=dir1 --with-gmp-lib=dir2’, where ‘dir1’ contains the GNU MP header files, and ‘dir2’ the GNU MP library files. If you get error messages, your machine might not be supported.

If you want to compile in a separate object directory, cd to that directory, and prefix the configure command with the path to the MPFR source directory. Not all ‘make’ programs have the necessary features to support this. In particular, SunOS and Slowaris ‘make’ have bugs that make them unable to build from a separate object directory. Use GNU ‘make’ instead. Also, the generated makefiles use some pattern matching rules that are not supported by all ‘make’ programs. Again, use GNU ‘make’ instead.

2. ‘make’

This will compile MPFR, and create a library archive file ‘libmpfr.a’ in the working directory.

3. ‘make check’

This will make sure MPFR was built correctly. If you get error messages, please report this to ‘mpfr@loria.fr’. (See Chapter 4 [Reporting Bugs], page 7, for information on what to include in useful bug reports.)

4. ‘make install’

This will copy the files ‘mpfr.h’ and ‘libmpfr.a’, to ‘/usr/local/include’ and ‘/usr/local/lib’ respectively (or if you passed the ‘--prefix’ option to ‘configure’, to the directory given as argument to ‘--prefix’).

There are some other useful make targets:

• ‘mpfr.dvi’

Create a DVI version of the manual, in ‘mpfr.dvi’.

• ‘mpfr.ps’

Create a Postscript version of the manual, in ‘mpfr.ps’.

• ‘clean’

Delete all object files and archive files, but not the configuration files.

• ‘distclean’

Delete all files not included in the distribution.

• ‘uninstall’ Delete all files copied by ‘make install’.

4 MPFR 1.0

2.1 Known Build Problems

MPFR suffers from all bugs from the GNU MP library, plus many many more. Please report other problems to ‘mpfr@loria.fr’. See Chapter 4 [Reporting Bugs], page 7.

3 MPFR Basics

All declarations needed to use MPFR are collected in the include file ‘mpfr.h’. It is designed to work with both C and C++ compilers. You should include that file in any program using the MPFR library:

#include "mpfr.h"

3.1 Nomenclature and Types

Floating point number or Float for short, is an arbitrary precision mantissa with a limited precision exponent. The C data type for such objects is mpfr_t.

The Precision is the number of bits used to represent the mantissa of a floating-point number; the corresponding C data type is mp_prec_t.

The rounding mode specifies the way to round the result of a floating-point operation, in case the exact result can not be represented exactly in the destination mantissa; the corresponding C data type is mp_rnd_t.

A limb means the part of a multi-precision number that fits in a single word. (We chose this word because a limb of the human body is analogous to a digit, only larger, and containing several digits.) Normally a limb contains 32 or 64 bits. The C data type for a limb is mp_limb_t.

3.2 Function Classes

There is only one class of functions in the MPFR library:

1. Functions for floating-point arithmetic, with names beginning with mpfr_. The associated type is mpfr_t. There are about 50 functions is this class.

3.3 MPFR Variable Conventions

As a general rule, all MPFR functions expect output arguments before input arguments. This notation is based on an analogy with the assignment operator.

MPFR allows you to use the same variable for both input and output in the same expression. For example, the main function for floating-point multiplication, mpfr_mul, can be used like this: mpfr_mul (x, x, x, rnd_mode). This computes the square of x with rounding mode rnd_mode and puts the result back in x.

Before you can assign to an MPFR variable, you need to initialize it by calling one of the special initialization functions. When you’re done with a variable, you need to clear it out, using one of the functions for that purpose.

A variable should only be initialized once, or at least cleared out between each initialization. After a variable has been initialized, it may be assigned to any number of times.

For efficiency reasons, avoid to initialize and clear out a variable in loops. Instead, initialize it before entering the loop, and clear it out after the loop has exited.

You don’t need to be concerned about allocating additional space for MPFR variables, since any variable has a mantissa of fixed size. Hence unless you change its precision, or clear and reinitialize it, a floating-point variable will have the same allocated space during all its life.

3.4 Getting the Latest Version of MP

The latest version of the MPFR library is available from the web page of the PolKA team at

‘http://www.loria.fr/equipes/polka’.

4 Reporting Bugs

If you think you have found a bug in the MPFR library, please investigate it and report it. We have made this library available to you, and it is not to ask too much from you, to ask you to report the bugs that you find.

There are a few things you should think about when you put your bug report together.

You have to send us a test case that makes it possible for us to reproduce the bug. Include instructions on how to run the test case.

You also have to explain what is wrong; if you get a crash, or if the results printed are incorrect and in that case, in what way.

Please include compiler version information in your bug report. This can be extracted using ‘what `which cc`’, or, if you’re using gcc, ‘gcc -v’. Also, include the output from ‘uname -a’.

If your bug report is good, we will do our best to help you to get a corrected version of the library; if the bug report is poor, we won’t do anything about it (aside of chiding you to send better bug reports).

Send your bug report to: ‘mpfr@loria.fr’.

If you think something in this manual is unclear, or downright incorrect, or if the language needs to be improved, please send a note to the same address.

5 Floating-point Functions

The floating-point functions expect arguments of type mpfr_t.

The MPFR floating-point functions have an interface that is similar to the GNU MP integer functions. The function prefix for floating-point operations is mpfr_.

There is one significant characteristic of floating-point numbers that has motivated a difference between this function class and other MPFR function classes: the inherent inexactness of floating point arithmetic. The user has to specify the precision of each variable. A computation that assigns a variable will take place with the precision of the assigned variable; the cost of that computation should not depend from the precision of variables used as input on average.

The precision of a calculation is defined as follows: Compute the requested operation exactly (with “infinite precision”), and round the result to the destination variable precision with the given rounding mode. Even if the user has asked for a very high precision, MP will not calculate with superfluous digits. For example, if two low-precision numbers of nearly equal magnitude are added, the precision of the result will be limited to what is required to represent the result accurately.

The MPFR floating-point functions are intended to be a smooth extension of the IEEE P754 arithmetic. The results obtained on one computer should not differ from the results obtained on a computer with a different word size.

5.1 Global Variables and Rounding Modes

Global Variable mp_prec_t gmp default fp bit precision

The default precision in bits (53 initially).

Global Variable mp_rnd_t gmp default rounding mode

The default rounding mode (to nearest initially).

The following four rounding modes are supported:

• GMP_RNDN: round to nearest

• GMP_RNDZ: round towards zero

• GMP_RNDU: round towards plus infinity

• GMP_RNDD: round towards minus infinity

The ‘round to nearest’ mode works as in the IEEE P754 standard: in case the number to be rounded lies exactly in the middle of two reprensentable numbers, it is rounded to the one with the least significant bit set to zero. For example, the number 5, which is represented by (101) in binary, is rounded to (100)=4 with a precision of two bits, and not to (110)=6. This rule avoids the drift phenomena mentioned by Knuth in volume 2 of The Art of Computer Programming (section 4.2.2, pages 221-222).

Function void mpfr set default rounding mode (mp_rnd_t rnd)

Sets the default rounding mode to rnd. The default rounding mode is to nearest initially.

Function void mpfr round (mpfr_t x, mp_rnd_t rnd, mp_prec_t prec)

Round x according to rnd with precision prec, which may be different from that of x. If prec is greater or equal to the precision of x, then new space is allocated for the mantissa, and it is filled with zeroes. Otherwise, the mantissa is rounded to precision prec with the given direction. In both cases, the precision of x is changed to prec.

Function void mpfr set machine rnd mode (mp_rnd_t rnd)

Set the machine rounding mode to rnd. This function is useful for debugging purpose, but also as a common interface to all different ways of setting the rounding mode, which unfortunately differ from one operating system to another one.

Function char * mpfr print rnd mode (mp_rnd_t rnd)

Returns the input string (GMP RNDD, GMP RNDU, GMP RNDN, GMP RNDZ) corresponding to the rounding mode rnd.

5.2 Initialization and Assignment Functions

Function void mpfr set default prec (mp_prec_t prec)

Set the default precision to be exactly prec bits. The precision of a variable means the number of bits used to store its mantissa. All subsequent calls to mpf_init will use this precision, but previously initialized variables are unaffected. This default precision is set to 53 bits initially. The precision can be any positive integer, even a precision of 1 is possible.

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.

Function void mpfr init (mpfr_t x)

Initialize x, and set its value to zero. 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 to mpfr_set_default_prec.

Function void mpfr init2 (mpfr_t x, mp_prec_t prec)

Initialize x, set its precision to be exactly prec bits, and set its value to zero. 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,

use mpfr_set_prec instead.

Function void mpfr clear (mpfr_t x)

Free the space occupied by x. Make sure to call this function for all mpfr_t variables when you are done with them.

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 */
       ...
       /* Unless the program is about to exit, do ... */
       mpfr_clear (x);
       mpfr_clear (y);
     }

The following two 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.

Function void mpfr set prec (mpfr_t x, mp_prec_t prec)

Reset the precision of x to be exactly prec bits. The previous value stored in x is lost. It is equivalent to a call to mpfr_clear(x) followed by a call to mpfr_init2(x, prec), but more efficient as no allocation is done in case the current allocated space for the mantissa of x is enough.

In case you want to keep the previous value stored in x, use mpfr_round instead.

Function mp_prec_t mpfr get prec (mpf_t x)

Return the precision actually used for assignments of x, i.e. the number of bits used to store its mantissa.

Function void mpfr set prec raw (mpf_t x, unsigned long int p)

Reset the precision of x to be exactly prec bits. The only difference with mpfr_set_prec is that p is assumed to be small enough so that the mantissa fits into the current allocated memory space for x. Otherwise an error will occur.

5.2.1 Assignment Functions

These functions assign new values to already initialized floats (see Section 5.2 [Initializing Floats], page 9).

Function void mpfr set (mpfr_t rop, mpfr_t op, mp_rnd_t rnd)
Function void mpfr set ui (mpfr_t rop, unsigned long int op, mp_rnd_t rnd)
Function void mpfr set si (mpfr_t rop, long int op, mp_rnd_t rnd
Function void mpfr set d (mpfr_t rop, double op, mp_rnd_t rnd)

Function void mpfr set z (mpfr_t rop, mpz_t op, mp_rnd_t rnd) Function void mpfr set q (mpfr_t rop, mpq_t op, mp_rnd_t rnd)

Set the value of rop from op, rounded to the precision of rop towards the given direction rnd. Please note that even a long int may have to be rounded, if the destination precision is less than the machine word width.

Function int mpfr set str (mpfr_t x, char *s, int base, mp_rnd_t rnd)

Set x to the value of the string s in base base, rounded in direction rnd to the precision of x. The exponent is read in decimal. This function returns 0 if the entire string up to the final ’\0’ is a valid number in base base, and −1 otherwise.

Function void mpfr set str raw (mpfr_t x, char *s)

Set x to the value of the binary number in string s, which has to be of the form +/- xxxx.xxxxxxEyy. The exponent is read in decimal, but is interpreted as the power of two to be multiplied by the mantissa. The mantissa length of s has to be less or equal to the precision of x, otherwise an error occurs. If s starts with N, it is interpreted as NaN (Not-a-Number); if it starts with I after the sign, it is interpreted as infinity, with the corresponding sign.

Function void mpfr set f (mpfr_t x, mpf_t y, mp_rnd_t rnd)

Set x to the GNU MP floating-point number y, rounded with the rnd mode and the precision of x.

Function void mpfr swap (mpfr_t x, mpf_t y)

Swap the values x and y efficiently. Warning: the precisions are exchanged too; in case the precisions are different, mpfr_swap is thus not equivalent to three mpfr_set calls using a third auxiliary variable.

5.2.2 Combined Initialization and Assignment Functions

Macro void mpfr init set (mpfr_t rop, mpfr_t op, mp_rnd_t rnd)
Macro void mpfr init set ui (mpfr_t rop, unsigned long int op, mp_rnd_t rnd)
Macro void mpfr init set si (mpfr_t rop, signed long int op, mp_rnd_t rnd)

Macro void mpfr init set d (mpfr_t rop, double op, mp_rnd_t rnd)

Initialize rop and set its value from op, rounded to direction rnd. The precision of rop will be taken from the active default precision, as set by mpfr_set_default_prec.

Function int mpfr init set str (mpfr_t x, char *s, int base, mp_rnd_t rnd)

Initialize x and sets its value from the string s in base base, rounded to direction rnd. See

mpfr_set_str.

5.3 Conversion Functions

Function double mpfr get d (mpfr_t op)

Convert op to a double, using the current machine rounding mode.

Function char * mpfr get str (char *str, mp_exp_t *expptr, int base, size_t

    n digits, mpfr_t op, mp_rnd_t rnd)

Convert op to a string of digits in base base, with rounding in direction rnd. The base may vary from 2 to 36. Generate exactly n digits significant digits.

If n digits is 0, it prints the maximum possible number of digits giving an exact rounding in the given base base with the direction rnd. In other words, if op was the exact rounding of a real number in direction rnd, then the printed value is also an exact rounding in base base of that real number with the same precision. An error occurs when one is unable to determine the leading digit, which can happen especially if the precision of op is small.

If str is NULL, space for the mantissa is allocated using the default allocation function, and a pointer to the string is returned. In that case, the user should her/himself free the corresponding memory with (*_mp_free_func)(s, strlen(s) + k) where k is 1 when the number is nonnegative, and 2 when there is a minus sign.

If str is not NULL, it should point to a block of storage enough large for the mantissa, i.e., n digits + 2. The two extra bytes are for a possible minus sign, and for the terminating null character.

The exponent is written through the pointer expptr.

If n digits is 0, note that the space requirements for str in this case will be impossible for the user to predetermine. Therefore, you need to pass NULL for the string argument whenever n digits is 0.

The generated string is a fraction, with an implicit radix point immediately to the left of the first digit. For example, the number 3.1416 would be returned as "31416" in the string and 1 written at expptr.

5.4 Basic Arithmetic Functions

Function void mpfr add (mpfr_t rop, mpfr_t op1, mpfr_t op2, mp_rnd_t rnd)
Function void mpfr add ui (mpfr_t rop, mpfr_t op1, unsigned long int op2,
    mp_rnd_t rnd)

Function void mpfr sub (mpfr_t rop, mpfr_t op1, mpfr_t op2, mp_rnd_t rnd)
Function void mpfr ui sub (mpfr_t rop, unsigned long int op1, mpfr_t op2,
    mp_rnd_t rnd)
Function void mpfr sub ui (mpfr_t rop, mpfr_t op1, unsigned long int op2,
    mp_rnd_t rnd)

Set rop to op1 − op2 rounded in the direction rnd.

Function void mpfr mul (mpfr_t rop, mpfr_t op1, mpfr_t op2, mp_rnd_t rnd)
Function void mpfr mul ui (mpfr_t rop, mpfr_t op1, unsigned long int op2,
    mp_rnd_t rnd)

Division is undefined if the divisor is zero, and passing a zero divisor to the divide functions will make these functions intentionally divide by zero. This gives the user the possibility to handle arithmetic exceptions in these functions in the same manner as other arithmetic exceptions.

Function void mpfr div (mpfr_t rop, mpfr_t op1, mpfr_t op2, mp_rnd_t rnd)
Function void mpfr ui div (mpfr_t rop, unsigned long int op1, mpfr_t op2,
    mp_rnd_t rnd)
Function int mpfr div ui (mpfr_t rop, mpfr_t op1, unsigned long int op2,
    mp_rnd_t rnd)

Set rop to op1/op2 rounded in the direction rnd. mpfr_div_ui returns 0 if the division is exact, a non-zero value otherwise.

Function int mpfr sqrt (mpfr_t rop, mpfr_t op, mp_rnd_t rnd)

Function int mpfr sqrt ui (mpfr_t rop, unsigned long int op, mp_rnd_t rnd)

Set rop to NaN if op is negative. Return 0 if the operation is exact, a non-zero value otherwise.

Function int mpfr pow ui (mpfr_t rop, mpfr_t op1, unsigned long int op2,
    mp_rnd_t rnd)
Function int mpfr ui pow ui (mpfr_t rop, unsigned long int op1, unsigned
    long int op2, mp_rnd_t rnd)

Set rop to op1 raised to op2. The computation is done by binary exponentiation, each multiplication being rounded to direction rnd. Hence the relative error with respect to the exact result may be larger than one unit in last place (ulp). In other words, this function does not provide an exact rounding. The returned value k is such that the error in ulps on the result is less than 2 power k.

Function void mpfr neg (mpfr_t rop, mpfr_t op, mp_rnd_t rnd)

Set rop to −op rounded in the direction rnd. Just changes the sign if rop and op are the same variable.

Function void mpfr abs (mpfr_t rop, mpfr_t op, mp_rnd_t rnd)

Set rop to the absolute value of op, rounded in the direction rnd.

Function void mpfr mul 2exp (mpfr_t rop, mpfr_t op1, unsigned long int op2,
    mp_rnd_t rnd)

rounded to the direction rnd. Just increases the exponent by op2 when rop and op1 are identical.

Function void mpfr div 2exp (mpfr_t rop, mpfr_t op1, unsigned long int op2,
    mp_rnd_t rnd)

rounded to the direction rnd. Just decreases the exponent by op2 when rop and op1 are identical.

5.5 Comparison Functions

Function int mpfr cmp (mpfr_t op1, mpfr_t op2)

Function int mpfr cmp ui (mpfr_t op1, unsigned long int op2)

Function int mpfr cmp si (mpfr_t op1, signed long int op2)

Both op1 and op2 are considered to their full own precision, which may differ. In case op1 and op2 are of same sign but different, the absolute value returned is one plus the absolute difference of their exponents. In case one of the operands is NaN (Not-a-Number), it returns 1, even if both are NaN.

Function int mpfr cmp ui 2exp (mpfr_t op1, unsigned long int op2, int e) Function int mpfr cmp si 2exp (mpfr_t op1, long int op2, int e)

Compare op1 and op2 multiplied by two to the power e.

Function int mpfr eq (mpfr_t op1, mpfr_t op2, unsigned long int op3)

Return non-zero if the first op3 bits of op1 and op2 are equal, zero otherwise. I.e., test of op1 and op2 are approximately equal.

Function void mpfr reldiff (mpfr_t rop, mpfr_t op1, mpfr_t op2, mp_rnd_t rnd)

Compute the relative difference between op1 and op2 and store the result in rop. This function does not guarantee the exact rounding on the relative difference; it just computes abs(op1-op2)/op1, using the rounding mode rnd for all operations.

Macro int mpfr sgn (mpfr_t op)

This function is actually implemented as a macro. It evaluates its arguments multiple times. It is provided for compatibility with mpf.

Macro int SIGN (mpfr_t op)

This function is actually implemented as a macro. It evaluates its arguments multiple times. As in the IEEE 754 standard, there is a positive zero and a negative one, therefore the SIGN macro does not distinguish zero from non-zero numbers.

Macro int MPFR NOTZERO (mpfr_t op)

Returns zero when its argument is zero, and a non-zero value otherwise.

5.6 Special Functions

Function int mpfr log (mpfr_t rop, mpfr_t op, mp_rnd_t rnd)

Set rop to the natural logarithm of op, rounded to the direction rnd with the precision of rop. Return 0 iff the result is exact (this occurs in fact only when op is 1 i.e. the result is 0).

Function int mpfr exp (mpfr_t rop, mpfr_t op, mp_rnd_t rnd)

Set rop to the exponential of op, rounded to the direction rnd with the precision of rop. Return 0 iff the result is exact (this occurs in fact only when op is 0 i.e. the result is 1).

Function int mpfr sin cos (mpfr_t sop, mpfr_t cop, mpfr_t op, mp_rnd_t rnd)

Set sop to the sinus of op, cop to the cosinus of op, rounded to the direction rnd with the precision of rop. sop and cop must be different variables. Return 0 iff the results are exact (this occurs in fact only when op is 0 i.e. the sine is 0 and the cosine is 1).

Function void mpfr agm (mpfr_t rop, mpfr_t op1, mpfr_t op2, mp_rnd_t rnd)

Set rop to the arithmetic-geometric mean of op1 and op2, rounded to the direction rnd with the precision of rop.

Function void mpfr const log2 (mpfr_t rop, mp_rnd_t rnd)

Set rop to the logarithm of 2 rounded to the direction rnd with the precision of rop. This function stores the computed value to avoid another calculation if a lower or equal precision is requested.

Function void mpfr const pi (mpfr_t rop, mp_rnd_t rnd)

Set rop to the value of Pi rounded to the direction rnd with the precision of rop. This function uses the Borwein, Borwein, Plouffe formula which directly gives the expansion of Pi in base 16.

5.7 Input and Output Functions

Functions that perform input from a stdio stream, and functions that output to a stdio stream. Passing a NULL pointer for a stream argument to any of these functions will make them read from stdin and write to stdout, respectively.

When using any of these functions, it is a good idea to include ‘stdio.h’ before ‘mpfr.h’, since that will allow ‘mpfr.h’ to define prototypes for these functions.

Function size_t mpfr out str (FILE *stream, int base, size_t n digits, mpfr_t

    op, mp_rnd_t rnd)

Output op on stdio stream stream, as a string of digits in base base, rounded to direction rnd. The base may vary from 2 to 36. Print at most n digits significant digits, or if n digits is 0, the maximum number of digits accurately representable by op.

In addition to the significant digits, a decimal point at the right of the first digit and a trailing exponent, in the form ‘eNNN’, are printed. If base is greater than 10, ‘@’ will be used instead of ‘e’ as exponent delimiter.

Return the number of bytes written, or if an error occurred, return 0.

Function size_t mpfr inp str (mpfr_t rop, FILE *stream, int base, mp_rnd_t

rnd) Input a string in base base from stdio stream stream, rounded in direction rnd, and put the read float in rop. The string is of the form ‘M@N’ or, if the base is 10 or less, alternatively ‘MeN’ or ‘MEN’. ‘M’ is the mantissa and ‘N’ is the exponent. The mantissa is always in the specified base. The exponent is in decimal.

The argument base may be in the ranges 2 to 36.

Unlike the corresponding mpz function, the base will not be determined from the leading characters of the string if base is 0. This is so that numbers like ‘0.23’ are not interpreted as octal.

Return the number of bytes read, or if an error occurred, return 0.

Function void mpfr print raw (mpfr_t float)

Output float on stdout in raw binary format (the exponent is in decimal, yet). The last bits from the least significant limb which do not belong to the mantissa are printed between square brackets; they should always be zero.

5.8 Miscellaneous Functions

Function void mpfr ceil (mpfr_t rop, mpfr_t op) Function void mpfr floor (mpfr_t rop, mpfr_t op) Function void mpfr trunc (mpfr_t rop, mpfr_t op)

Set rop to op rounded to an integer. mpfr_ceil rounds to the next higher integer, mpfr_ floor to the next lower, and mpfr_trunc to the integer towards zero.

Function void mpfr urandomb (mpfr_t rop, gmp_randstate_t state)

Generate a uniformly distributed random float in the interval 0 <= X < 1.

Function void mpfr random (mpfr_t rop)

Put in rop a random float in the interval [0,1[. Random limbs are generated using the random system function. zeros and ones in the binary representation. The exponent of the number is in the interval −exp to exp. This function is useful for testing functions and algorithms, since this kind of random numbers have proven to be more likely to trigger corner-case bugs. Negative random numbers are generated when max size is negative.

Function void mpfr srandom (unsigned long int seed)

Set the random seed used by mpfr_random to seed.

Function void mpfr random2 (mpfr_t rop, mp_size_t max size, mp_exp_t

max exp) Generate a random float of at most max size limbs, with long strings of zeros and ones in the binary representation. The exponent of the number is in the interval −exp to exp. Negative random numbers are generated when max size is negative.

5.9 Internal Functions

These functions were mainly designed for the implementation of mpfr, but may be useful for users too. You need to include mpfr-impl.h to use them.

Function Macro int MPFR_IS_NAN (mpfr_t x)

Returns a non-zero value iff its argument is ‘Not a Number’.

Function Macro int MPFR_SET_NAN (mpfr_t x)

Sets its argument to ‘Not a Number’.

Function Macro int MPFR_ABSSIZE (mpfr_t x)

Returns the number of limb the mantissa of x points to.

Function Macro int MPFR_EXP (mpfr_t x)

Returns the exponent of x.

Function Macro mp limb t* MPFR_MANT (mpfr_t x)

Returns a pointer to the mantissa of x.

Function Macro mp prec t MPFR_PREC (mpfr_t x)

Returns the precision of x.

Function Macro int MPFR_CHANGE_SIGN (mpfr_t x)

Changes the sign of x.

Function int mpfr add one ulp (mpfr_t x)

Add one unit in last place (ulp) to the mantissa of x if it is positive or zero, and subtracts one ulp otherwise. Always return 0 (result is exact).

Function int mpfr sub one ulp (mpfr_t x)

Subtract one ulp to x if it is positive or zero, and adds one ulp otherwise. Always return 0 (result is exact).

Function int mpfr round raw (mp_limb_t* y, mp_limb_t* x, mp_prec_t xprec,
    int neg, mp_prec_t yprec, mp_rnd_t rnd)

Puts in mantissa beginning at y the value of that beginning at x (with precision xprec bits and negative iff neg is not zero) rounded with mode rnd to precision yprec.

Function int mpfr round raw2 (mp_limb_t* x, mp_prec_t xn, int neg, mp_rnd_t
    rnd, mp_prec_t prec)

Returns 0 if the multiple precision number stored in the xn limbs starting at xp (least significant limbs first) with sign neg (positive iff neg equals zero) is rounded similarly with direction rnd and precision prec than towards zero, and 1 otherwise. In other words, the bit returned is what has to be added to bit prec to round to mode rnd. This function does not modify the mantissa stored in xp.

Function int mpfr can round (mpfr_t b, mp_prec_t err, mp_rnd_t rnd1,
    mp_rnd_t rnd2, mp_prec_t prec)

Assuming b is an approximation of an unknown number x in direction rnd1 with error at most two to the power E(b)-err where E(b) is the exponent of b, returns 1 if one is able to round exactly x to precision prec with direction rnd2, and 0 otherwise. This function does not modify its arguments.

Contributors

We would like to thank Jean-Michel Muller and Joris van der Hoeven for very fruitful discussions at the beginning of that project, Torbjorn Granlund for his help about design issues and his suggestions for a easy integration into GNU MP.

Sylvie Boldo from ENS-Lyon, France, contributed the functions mpfr_agm and mpfr_log. Emmanuel Jeandel, from ENS-Lyon too, contributed the generic hypergeometric code in generic.c, as well as the mpfr_exp3, the mpfr_sin_cos functions, and improved versions of mpfr_const_

log2 and mpfr_const_pi.

Thom Mulders (ETH Z\"urich) contributed the routines for short multiplication.

References

• Torbjorn Granlund, "GNU MP: The GNU Multiple Precision Arithmetic Library", version 3.1, 2000.

• IEEE standard for binary floating-point arithmetic, Technical Report ANSI-IEEE Standard 754-1985, New York, 1985. Approved March 21, 1985: IEEE Standards Board; approved July 26, 1985: American National Standards Institute, 18 pages.

• Donald E. Knuth, "The Art of Computer Programming", vol 2, "Seminumerical Algorithms", 2nd edition, Addison-Wesley, 1981.

20 MPFR 1.0

Concept Index

A

Arithmetic functions . . . . . . . . . . . . . . . . . . . . . . . . . . . 12

C

Comparison functions . . . . . . . . . . . . . . . . . . . . . . . . . . 13 Conditions for copying MPFR. . . . . . . . . . . . . . . . . . . . 1 Conversion functions . . . . . . . . . . . . . . . . . . . . . . . . . . . 11 Copying conditions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1

F

Float arithmetic functions . . . . . . . . . . . . . . . . . . . . . . 12 Float assignment functions. . . . . . . . . . . . . . . . . . . . . . 10 Float comparisons functions. . . . . . . . . . . . . . . . . . . . . 13 Float functions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8 Float input and output functions . . . . . . . . . . . . . . . . 15 Floating-point functions . . . . . . . . . . . . . . . . . . . . . . . . . 8 Floating-point number. . . . . . . . . . . . . . . . . . . . . . . . . . . 5

I

I/O functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15 Initialization and assignment functions. . . . . . . . . . . 11 Input functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15 Installation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3

L

Limb . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5

M

Miscellaneous float functions . . . . . . . . . . . . . . . . . . . . 16 ‘mpfr.h’ . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5

O

Output functions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15

P

Precision . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5

R

Reporting bugs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7 Rouding modes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8 Rounding Mode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5

S

Special functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14

U

User-defined precision . . . . . . . . . . . . . . . . . . . . . . . . . . . 8

Function and Type Index 21

Function and Type Index

I

int . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16, 17

M

mp_limb_t* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17 mp_prec_t . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5, 17 mp_rnd_t . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5 mpfr_abs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13 mpfr_add . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12 mpfr_add_one_ulp . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17 mpfr_add_ui. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12 mpfr_agm . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15 mpfr_can_round . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17 mpfr_ceil . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16 mpfr_clear . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9 mpfr_cmp . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13 mpfr_cmp_si. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13 mpfr_cmp_si_2exp . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14 mpfr_cmp_ui. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13 mpfr_cmp_ui_2exp . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14 mpfr_const_log2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15 mpfr_const_pi. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15 mpfr_div . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12 mpfr_div_2exp. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13 mpfr_div_ui. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13 mpfr_eq . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14 mpfr_exp . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14 mpfr_floor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16 mpfr_get_d . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11 mpfr_get_prec. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10 mpfr_get_str. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11 mpfr_init . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9 mpfr_init_set. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11 mpfr_init_set_d . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11 mpfr_init_set_si . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11 mpfr_init_set_str . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11 mpfr_init_set_ui . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11 mpfr_init2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9 mpfr_inp_str. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15 mpfr_log . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14 mpfr_mul . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12 mpfr_mul_2exp. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13 mpfr_mul_ui. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12 mpfr_neg . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13 MPFR_NOTZERO. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14 mpfr_out_str. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15 mpfr_pow_ui. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13 mpfr_print_raw . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16 mpfr_print_rnd_mode . . . . . . . . . . . . . . . . . . . . . . . . . . . 9 mpfr_random. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16 mpfr_random2. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16 mpfr_reldiff. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14 mpfr_round . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9 mpfr_round_raw . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17 mpfr_round_raw2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17 mpfr_set . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10 mpfr_set_d . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10 mpfr_set_default_prec . . . . . . . . . . . . . . . . . . . . . . . . . 9

mpfr_set_default_rounding_mode . . . . . . . . . . . . . . . 8

mpfr_set_f . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11

mpfr_set_machine_rnd_mode. . . . . . . . . . . . . . . . . . . . . 9

mpfr_set_prec. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10 mpfr_set_prec_raw . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10 mpfr_set_q . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10 mpfr_set_si. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10 mpfr_set_str. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11 mpfr_set_str_raw . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11 mpfr_set_ui. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10 mpfr_set_z . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10 mpfr_sgn . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14 mpfr_sin_cos. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14 mpfr_sqrt . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13 mpfr_sqrt_ui. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13 mpfr_srandom. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16 mpfr_sub . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12 mpfr_sub_one_ulp . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17 mpfr_sub_ui. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12 mpfr_swap . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11 mpfr_t . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5 mpfr_trunc . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16 mpfr_ui_div. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13 mpfr_ui_pow_ui . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13 mpfr_ui_sub. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12 mpfr_urandomb. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16

S

SIGN . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14

22 MPFR 1.0

Table of Contents

MPFR Copying Conditions . . . . . . . . . . . . . . . . . . . . . . . 1

1 Introduction to MPFR . . . . . . . . . . . . . . . . . . . . . . . . 2

1.1 How to use this Manual . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2

2 Installing MPFR . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3

2.1 Known Build Problems . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4

3 MPFR Basics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5

3.1 Nomenclature and Types. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5 3.2 Function Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5 3.3 MPFR Variable Conventions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5 3.4 Getting the Latest Version of MP. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6

4 Reporting Bugs. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7

5 Floating-point Functions . . . . . . . . . . . . . . . . . . . . . . 8

5.1 Global Variables and Rounding Modes . . . . . . . . . . . . . . . . . . . . . . . . 8 5.2 Initialization and Assignment Functions . . . . . . . . . . . . . . . . . . . . . . . 9

5.2.1 Assignment Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10 5.2.2 Combined Initialization and Assignment Functions … 11

5.3 Conversion Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11 5.4 Basic Arithmetic Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12 5.5 Comparison Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13 5.6 Special Functions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14 5.7 Input and Output Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15 5.8 Miscellaneous Functions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16 5.9 Internal Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16

Contributors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18

References . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19

Concept Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20

Function and Type Index. . . . . . . . . . . . . . . . . . . . . . . . 21