This chapter describes the GMP functions for performing floating point
arithmetic. These functions start with the prefix mpf_
.
GMP floating point numbers are stored in objects of type mpf_t
.
The GMP floating-point functions have an interface that is similar to the GMP
integer functions. The function prefix for floating-point operations is
mpf_
.
There is one significant characteristic of floating-point numbers that has motivated a difference between this function class and other GMP 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 precision of variables used as input is ignored.
The precision of a calculation is defined as follows: Compute the requested operation exactly (with "infinite precision"), and truncate the result to the destination variable precision. Even if the user has asked for a very high precision, GMP 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 GMP floating-point functions are not intended as a smooth extension to the IEEE P754 arithmetic. Specifically, the results obtained on one computer often differs from the results obtained on a computer with a different word size.
mpf_init
will use this precision, but previously
initialized variables are unaffected.
An mpf_t
object must be initialized before storing the first value in
it. The functions mpf_init
and mpf_init2
are used for that
purpose.
mpf_clear
, between initializations. The
precision of x is undefined unless a default precision has already been
established by a call to mpf_set_default_prec
.
mpf_clear
, between initializations.
mpf_t
variables when you are done with them.
Here is an example on how to initialize floating-point variables:
{ mpf_t x, y; mpf_init (x); /* use default precision */ mpf_init2 (y, 256); /* precision at least 256 bits */ ... /* Unless the program is about to exit, do ... */ mpf_clear (x); mpf_clear (y); }
The following three 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.
realloc
, this routine
should not be called in a tight loop.
mpf_get_prec
. It is crucial that the precision of rop is
ultimately reset to exactly the value returned by mpf_get_prec
before
the first call to mpf_set_prec_raw
.
These functions assign new values to already initialized floats (see section Initialization Functions).
The argument base may be in the ranges 2 to 36, or -36 to -2. Negative values are used to specify that the exponent is in decimal.
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.
White space is allowed in the string, and is simply ignored. [This is not really true; white-space is ignored in the beginning of the string and within the mantissa, but not in other places, such as after a minus sign or in the exponent. We are considering changing the definition of this function, making it fail when there is any white-space in the input, since that makes a lot of sense. Please tell us your opinion about this change. Do you really want it to accept "3 14" as meaning 314 as it does now?]
This function returns 0 if the entire string up to the '\0' is a valid number in base base. Otherwise it returns -1.
For convenience, GMP provides a parallel series of initialize-and-set functions
which initialize the output and then store the value there. These functions'
names have the form mpf_init_set...
Once the float has been initialized by any of the mpf_init_set...
functions, it can be used as the source or destination operand for the ordinary
float functions. Don't use an initialize-and-set function on a variable
already initialized!
The precision of rop will be taken from the active default precision, as
set by mpf_set_default_prec
.
mpf_set_str
above for details on the assignment operation.
Note that rop is initialized even if an error occurs. (I.e., you have to
call mpf_clear
for it.)
The precision of rop will be taken from the active default precision, as
set by mpf_set_default_prec
.
If str is NULL
, space for the mantissa is allocated using the default
allocation function.
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, the maximum number of digits meaningfully achievable
from the precision of op will be generated. 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.
A pointer to the result string is returned. This pointer will will either
equal str, or if that is NULL
, will point to the allocated storage.
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 lets the user handle arithmetic exceptions in these functions in the same manner as other arithmetic exceptions.
This function is actually implemented as a macro. It evaluates its arguments multiple times.
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 `gmp.h', since that will allow `gmp.h' to define prototypes for these functions.
In addition to the significant digits, a leading `0.' 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.
The argument base may be in the ranges 2 to 36, or -36 to -2. Negative values are used to specify that the exponent is in decimal.
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.
mpf_ceil
rounds to
the next higher integer, mpf_floor
to the next lower, and
mpf_trunc
to the integer towards zero.
The variable state must be initialized by calling one of the
gmp_randinit
functions (section Random State Initialization)
before invoking this function.
Go to the first, previous, next, last section, table of contents.