Oracle® Solaris Studio 12.4: Numerical Computation Guide

Exit Print View

Updated: January 2015
 
 

3.5.1 Exception Flag Functions

The fenv.h file defines macros for each of the five IEEE floating-point exception flags: FE_INEXACT, FE_UNDERFLOW, FE_OVERFLOW, FE_DIVBYZERO, and FE_INVALID. In addition, the macro FE_ALL_EXCEPT is defined to be the bitwise “or” of all five flag macros. In the following descriptions, the excepts parameter might be a bitwise “or” of any of the five flag macros or the value FE_ALL_EXCEPT. For the fegetexceptflag and fesetexceptflag functions, the flagp parameter must be a pointer to an object of type fexcept_t. This type is defined in fenv.h.

C99 defines the exception flag functions in the following table:

Table 3-15  C99 Standard Exception Flag Functions
Function
Action
feclearexcept(excepts)
clear specified flags
fetestexcept(excepts)
return settings of specified flags
feraiseexcept(excepts)
raise specified exceptions
fegetexceptflag(flagp, excepts)
save specified flags in *flagp
fesetexceptflag(flagp, excepts)
restore specified flags from *flagp

The feclearexcept function clears the specified flags. The fetestexcept function returns a bitwise “or” of the macro values corresponding to the subset of flags specified by the excepts argument that are set. For example, if the only flags currently set are inexact, underflow, and division by zero, then the following would set i to FE_DIVBYZERO.

i = fetestexcept(FE_INVALID | FE_DIVBYZERO);

The feraiseexcept function causes a trap if any of the specified exceptions' trap is enabled. Otherwise, it merely sets the corresponding flags. See Chapter 4, Exceptions and Exception Handling for more information on exception traps.

The fegetexceptflag and fesetexceptflag functions provide a convenient way to temporarily save the state of certain flags and later restore them. In particular, the fesetexceptflag function does not cause a trap; it merely restores the values of the specified flags.