7.6 Floating-point environment <fenv.h>
The header <fenv.h> declares two types and several macros and functions to provide access to the floating-point environment. The floating-point environment refers collectively to any floating-point status flags and control modes supported by the implementation.[1] A floating-point status flag is a system variable whose value is set (but never cleared) when a floating-point exception is raised, which occurs as a side effect of exceptional floating-point arithmetic to provide auxiliary information.[2] A floating- point control mode is a system variable whose value may be set by the user to affect the subsequent behavior of floating-point arithmetic.
Certain programming conventions support the intended model of use for the floating- point environment:[3]
a function call does not alter its caller’s floating-point control modes, clear its caller’s floating-point status flags, nor depend on the state of its caller’s floating-point status flags unless the function is so documented;
a function call is assumed to require default floating-point control modes, unless its documentation promises otherwise;
a function call is assumed to have the potential for raising floating-point exceptions, unless its documentation promises otherwise.
The type
fenv_t
represents the entire floating-point environment.
The type
fexcept_t
represents the floating-point status flags collectively, including any status the implementation associates with the flags.
Each of the macros
FE_DIVBYZERO
FE_INEXACT
FE_INVALID
FE_OVERFLOW
FE_UNDERFLOW
is defined if and only if the implementation supports the floating-point exception by means of the functions in 7.6.2.[4] Additional implementation-defined floating-point exceptions, with macro definitions beginning with FE_ and an uppercase letter, may also be specified by the implementation. The defined macros expand to integer constant expressions with values such that bitwise ORs of all combinations of the macros result in distinct values, and furthermore, bitwise ANDs of all combinations of the macros result in zero.[5]
The macro
FE_ALL_EXCEPT
is simply the bitwise OR of all floating-point exception macros defined by the implementation. If no such macros are defined, FE_ALL_EXCEPT shall be defined as 0.
Each of the macros
FE_DOWNWARD
FE_TONEAREST
FE_TOWARDZERO
FE_UPWARD
is defined if and only if the implementation supports getting and setting the represented rounding direction by means of the fegetround and fesetround functions. Additional implementation-defined rounding directions, with macro definitions beginning with FE_ and an uppercase letter, may also be specified by the implementation. The defined macros expand to integer constant expressions whose values are distinct nonnegative values.[6]
The macro
FE_DFL_ENV
represents the default floating-point environment – the one installed at program startup – and has type “pointer to const-qualified fenv_t”. It can be used as an argument to <fenv.h> functions that manage the floating-point environment.
Additional implementation-defined environments, with macro definitions beginning with FE_ and an uppercase letter, and having type “pointer to const-qualified fenv_t”, may also be specified by the implementation.
Footnotes