Go to the first, previous, next, last section, table of contents.


Floating-Point Control Functions

IEEE 754 floating-point implementations allow the programmer to decide whether traps will occur for each of the exceptions, by setting bits in the control word. In C, traps result in the program receiving the SIGFPE signal; see section Signal Handling.

Note: IEEE 754 says that trap handlers are given details of the exceptional situation, and can set the result value. C signals do not provide any mechanism to pass this information back and forth. Trapping exceptions in C is therefore not very useful.

It is sometimes necessary to save the state of the floating-point unit while you perform some calculation. The library provides functions which save and restore the exception flags, the set of exceptions that generate traps, and the rounding mode. This information is known as the floating-point environment.

The functions to save and restore the floating-point environment all use a variable of type fenv_t to store information. This type is defined in `fenv.h'. Its size and contents are implementation-defined. You should not attempt to manipulate a variable of this type directly.

To save the state of the FPU, use one of these functions:

Function: void fegetenv (fenv_t *envp)
Store the floating-point environment in the variable pointed to by envp.

Function: int feholdexcept (fenv_t *envp)
Store the current floating-point environment in the object pointed to by envp. Then clear all exception flags, and set the FPU to trap no exceptions. Not all FPUs support trapping no exceptions; if feholdexcept cannot set this mode, it returns zero. If it succeeds, it returns a nonzero value.

The functions which restore the floating-point environment can take two kinds of arguments:

To set the floating-point environment, you can use either of these functions:

Function: void fesetenv (const fenv_t *envp)
Set the floating-point environment to that described by envp.

Function: void feupdateenv (const fenv_t *envp)
Like fesetenv, this function sets the floating-point environment to that described by envp. However, if any exceptions were flagged in the status word before feupdateenv was called, they remain flagged after the call. In other words, after feupdateenv is called, the status word is the bitwise OR of the previous status word and the one saved in envp.


Go to the first, previous, next, last section, table of contents.