# <fenv.h>
<fenv.h>
# ``
[added with
[C99](https://docs.qualcomm.com/doc/80-N3040-23/topic/c_lib_over.html#C99)]
* * *
**`FE_ALL_EXCEPT· FE_DFL_ENV· FE_DIVBYZERO· FE_DOWNWARD· FE_INEXACT· FE_INVALID· FE_OVERFLOW· FE_TONEAREST· FE_TOWARDZERO· FE_UNDERFLOW· FE_UPWARD`**
**`FENV_ACCESS`**
**`feclearexcept· fegetenv· fegetexceptflag· fegetround· feholdexcept· fenv_t· feraiseexcept· fesetenv· fesetexceptflag· fesetround· fetestexcept· feupdateenv· fexcept_t`**
**`fegettrapenable· fesettrapenable`**
* * *
Include the standard header **``**
to define two types, several macros, and several functions
that test and control floating-point status, if the implementation permits.
The functionality matches IEC 60559, but it can
be applied to similar floating-point hardware.
**Floating-point status**
can be represented in an object of type
`fexcept_t`.
It forms part of the
**floating-point control**,
which determines the behavior of floating-point arithmetic. A copy of the
floating-point control can be represented in an object of type
`fenv_t`.
Another part of the floating-point control is the
**rounding mode**,
representable as a value of type *int,* which determines
how floating-point values convert to integers. The rounding modes
are:
- **downward**, toward the nearest more negative integer
- **to nearest**, toward the nearest integer with the closer value,
or toward the nearest even integer if two integers are equally near
- **toward zero**, toward the nearest integer closer to zero
(also called truncation)
- **upward**, toward the nearest more positive integer
An implementation may define additional rounding modes.
By convention, a C function does not alter the floating-point control,
nor assume other than the default settings for the floating-point control,
without explicitly documenting the fact.
Any C function can change the floating-point status by
reporting one of several floating-point exceptions:
- An **inexact floating-point** exception
can occur when a finite floating-point result cannot be exactly represented, as in
`2.0 / 3.0`.
- An **invalid floating-point** exception
can occur when a floating-point operation involves an invalid combination of
operators and operands, as in `0.0 / 0.0`.
- An **overflow floating-point** exception
can occur when the magnitude of a finite floating-point result is too large to represent,
as in `DBL_MAX / DBL_MIN`.
- An **underflow floating-point** exception
can occur when the magnitude of a finite floating-point result is too small to represent,
as in `DBL_MIN / DBL_MAX`.
- A **zero-divide floating-point** exception
can occur when a floating-point divide has a finite dividend and a zero divisor, as in
`1.0 / 0.0`.
An implementation may define additional floating-point exceptions.
Reporting an exception sets a corresponding indicator in the floating-point
status. It can also **raise**
a floating-point exception, which can result in a hardware trap
and/or the raising of a [signal](https://docs.qualcomm.com/doc/80-N3040-23/topic/c_signal.html#signals).
The pragma:
#pragma STD FENV_ACCESS [ON|OFF|DEFAULT]Copy to clipboard
informs the translator whether the program intends to control and
test floating-point status. If the parameter is
`ON`, the program may use the functions declared in this header
to control and test floating-point status.
If the parameter is `OFF`, the use of these functions is disallowed.
The parameter `DEFAULT` restores the original state,
which is implementation defined.
If the pragma occurs outside an external declaration,
it remains in effect until overridden by another such pragma. If the pragma occurs
inside an external declaration, it must precede all explicit declarations and
statements within a compound statement. It remains in effect until overridden by
another such pragma or until the end of the compound statement.
On a transition from `OFF` to `ON`,
floating-point status flags are unspecified and the floating-point
control is in its default state.
/* MACROS */
#define FE_DIVBYZERO [optional]
#define FE_INEXACT [optional]
#define FE_INVALID [optional]
#define FE_OVERFLOW [optional]
#define FE_UNDERFLOW [optional]
#define FE_ALL_EXCEPT [optional]
#define FE_DOWNWARD [optional]
#define FE_TONEAREST [optional]
#define FE_TOWARDZERO [optional]
#define FE_UPWARD [optional]
#define FE_DFL_ENV
/* TYPES */
typedef o-type fenv_t;
typedef i-type fexcept_t;
/* FUNCTIONS */
int feclearexcept(int except);
int fegetexceptflag(fexcept_t *pflag, int except);
int feraiseexcept(int except);
int fesetexceptflag(const fexcept_t *pflag, int except);
int fetestexcept(int except);
int fegetround(void);
int fesetround(int mode);
int fegetenv(fenv_t *penv);
int feholdexcept(fenv_t *penv);
int fesetenv(const fenv_t *penv);
int feupdateenv(const fenv_t *penv);
fexcept_t fegettrapenable(void); [non-standard]
int fesettrapenable(fexcept_t enables); [non-standard]Copy to clipboard
## `FE_ALL_EXCEPT`
#define FE_ALL_EXCEPT [optional]Copy to clipboard
The macro expands to an integer value that, when ANDed with a value of type
`fexcept_t`, yields a nonzero value only
if the indicator is set for one or more floating-point exceptions.
The macro is not defined if the functions declared in this header cannot
control floating-point exceptions.
## `FE_DFL_ENV`
#define FE_DFL_ENV Copy to clipboard
The macro expands to a pointer to an object that describes
the settings for the floating-point control at
[program startup](https://docs.qualcomm.com/doc/80-N3040-23/topic/c_lib_over.html#program%20startup).
## `FE_DIVBYZERO`
#define FE_DIVBYZERO [optional]Copy to clipboard
The macro expands to an integer value that, when ANDed with a value of type
`fexcept_t`, yields a nonzero value only
if the indicator is set for a
[zero-divide floating-point](https://docs.qualcomm.com/doc/80-N3040-23/topic/c_fenv.html#zero-divide%20floating-point) exception.
The macro is not defined if the functions declared in this header cannot
control floating-point exceptions.
## `FE_DOWNWARD`
#define FE_DOWNWARD [optional]Copy to clipboard
The macro expands to an integer value accepted as an argument to
`fesetround`
and returned by
`fegetround`
to indicate the **downward**
[rounding mode](https://docs.qualcomm.com/doc/80-N3040-23/topic/c_fenv.html#rounding%20mode).
The macro is not defined if the functions declared in this header cannot
control the rounding mode.
## `FE_INEXACT`
#define FE_INEXACT [optional]Copy to clipboard
The macro expands to an integer value that, when ANDed with a value of type
`fexcept_t`, yields a nonzero value only
if the indicator is set for an
[inexact floating-point](https://docs.qualcomm.com/doc/80-N3040-23/topic/c_fenv.html#inexact%20floating-point) exception.
The macro is not defined if the functions declared in this header cannot
control floating-point exceptions.
## `FE_INVALID`
#define FE_INVALID [optional]Copy to clipboard
The macro expands to an integer value that, when ANDed with a value of type
`fexcept_t`, yields a nonzero value only
if the indicator is set for an
[invalid floating-point](https://docs.qualcomm.com/doc/80-N3040-23/topic/c_fenv.html#invalid%20floating-point) exception.
The macro is not defined if the functions declared in this header cannot
control floating-point exceptions.
## `FE_TONEAREST`
#define FE_TONEAREST [optional]Copy to clipboard
The macro expands to an integer value accepted as an argument to
`fesetround`
and returned by
`fegetround`
to indicate the **to nearest**
[rounding mode](https://docs.qualcomm.com/doc/80-N3040-23/topic/c_fenv.html#rounding%20mode).
The macro is not defined if the functions declared in this header cannot
control the rounding mode.
## `FE_TOWARDZERO`
#define FE_TOWARDZERO [optional]Copy to clipboard
The macro expands to an integer value accepted as an argument to
`fesetround`
and returned by
`fegetround`
to indicate the **toward zero**
[rounding mode](https://docs.qualcomm.com/doc/80-N3040-23/topic/c_fenv.html#rounding%20mode).
The macro is not defined if the functions declared in this header cannot
control the rounding mode.
## `FE_OVERFLOW`
#define FE_OVERFLOW [optional]Copy to clipboard
The macro expands to an integer value that, when ANDed with a value of type
`fexcept_t`, yields a nonzero value only
if the indicator is set for an
[overflow floating-point](https://docs.qualcomm.com/doc/80-N3040-23/topic/c_fenv.html#overflow%20floating-point) exception.
The macro is not defined if the functions declared in this header cannot
control floating-point exceptions.
## `FE_UNDERFLOW`
#define FE_UNDERFLOW [optional]Copy to clipboard
The macro expands to an integer value that, when ANDed with a value of type
`fexcept_t`, yields a nonzero value only
if the indicator is set for an
[underflow floating-point](https://docs.qualcomm.com/doc/80-N3040-23/topic/c_fenv.html#underflow%20floating-point) exception.
The macro is not defined if the functions declared in this header cannot
control floating-point exceptions.
## `FE_UPWARD`
#define FE_UPWARD [optional]Copy to clipboard
The macro expands to an integer value accepted as an argument to
`fesetround`
and returned by
`fegetround`
to indicate the **upward**
[rounding mode](https://docs.qualcomm.com/doc/80-N3040-23/topic/c_fenv.html#rounding%20mode).
The macro is not defined if the functions declared in this header cannot
control the rounding mode.
## `fenv_t`
typedef o-type fenv_t;Copy to clipboard
The type is an object type `o-type`
that can represent the settings stored in the
[floating-point control](https://docs.qualcomm.com/doc/80-N3040-23/topic/c_fenv.html#floating-point%20control).
## `feclearexcept`
int feclearexcept(int except);Copy to clipboard
The function attempts to clear
the exceptions selected by `except` in the
[floating-point status](https://docs.qualcomm.com/doc/80-N3040-23/topic/c_fenv.html#floating-point%20status) portion of the
[floating-point control](https://docs.qualcomm.com/doc/80-N3040-23/topic/c_fenv.html#floating-point%20control).
It returns zero only if `except` is zero or all the
exceptions selected by `except` are successfully cleared.
## `fegettrapenable`
fexcept_t fegettrapenable(void); [non-standard]Copy to clipboard
The function returns the current
floating-point enable mask from the
[floating-point control](https://docs.qualcomm.com/doc/80-N3040-23/topic/c_fenv.html#floating-point%20control),
or `(fexcept_t)(-1)` if it cannot be determined.
For an exception selected by `except` (such as `FE_OVERFLOW`),
an operation that [raises](https://docs.qualcomm.com/doc/80-N3040-23/topic/c_fenv.html#raise%20a%20floating-point%20exception)
the exception results in a hardware trap and/or the raising of a
[signal](https://docs.qualcomm.com/doc/80-N3040-23/topic/c_signal.html#signals)
only if `fegettrapenable() & except` is nonzero.
At [program startup](https://docs.qualcomm.com/doc/80-N3040-23/topic/c_lib_over.html#program%20startup),
`fegettrapenable()` returns zero.
## `fegetenv`
int fegetenv(fenv_t *penv);Copy to clipboard
The function attempts to store the settings in the
[floating-point control](https://docs.qualcomm.com/doc/80-N3040-23/topic/c_fenv.html#floating-point%20control)
at `*penv`. It returns zero only if the store succeeds.
## `fegetexceptflag`
int fegetexceptflag(fexcept_t *pflag, int except);Copy to clipboard
The function attempts to store in `*pflag` a representation of
the exceptions selected by `except` from the
[floating-point status](https://docs.qualcomm.com/doc/80-N3040-23/topic/c_fenv.html#floating-point%20status) portion of the
[floating-point control](https://docs.qualcomm.com/doc/80-N3040-23/topic/c_fenv.html#floating-point%20control).
It returns zero only if `except` is zero or all the
exceptions selected by `except` are successfully stored.
## `fegetround`
int fegetround(void);Copy to clipboard
The function returns the current
[rounding mode](https://docs.qualcomm.com/doc/80-N3040-23/topic/c_fenv.html#rounding%20mode) from the
[floating-point control](https://docs.qualcomm.com/doc/80-N3040-23/topic/c_fenv.html#floating-point%20control),
or a negative value if it cannot be determined.
## `feholdexcept`
int feholdexcept(fenv_t *penv);Copy to clipboard
The function stores the settings in the
[floating-point control](https://docs.qualcomm.com/doc/80-N3040-23/topic/c_fenv.html#floating-point%20control)
at `*penv`. It also clears all exceptions in the
[floating-point status](https://docs.qualcomm.com/doc/80-N3040-23/topic/c_fenv.html#floating-point%20status) portion of the
floating-point control and endeavors to establish settings that will not
[raise](https://docs.qualcomm.com/doc/80-N3040-23/topic/c_fenv.html#raise%20a%20floating-point%20exception) any exceptions. (The
effect is equivalent to calling
`fegetenv(penv)` followed by
`feclearexcept(FE_ALL_EXCEPT)` and
`fesettrapenable(0)`.) The function returns
zero only if it succeeds in establishing such settings.
You can use this function in conjunction with
`feupdateenv`
to defer the raising of exceptions until spurious ones are cleared, as in:
fenv_t env;
feholdexcept(&env); // save environment
// may accumulate exceptions
feclearexcept(FE_INEXACT); // clear unwanted exception
feupdateenv(&env); // raise any remaining exceptionsCopy to clipboard
## `feraiseexcept`
int feraiseexcept(int except);Copy to clipboard
The function attempts to
[raise](https://docs.qualcomm.com/doc/80-N3040-23/topic/c_fenv.html#raise%20a%20floating-point%20exception) the
floating-point exceptions specified by `except`.
Whether it raises an
[inexact floating-point](https://docs.qualcomm.com/doc/80-N3040-23/topic/c_fenv.html#inexact%20floating-point) exception
after an
[overflow floating-point](https://docs.qualcomm.com/doc/80-N3040-23/topic/c_fenv.html#overflow%20floating-point) exception or an
[underflow floating-point](https://docs.qualcomm.com/doc/80-N3040-23/topic/c_fenv.html#underflow%20floating-point) exception
is implementation defined. It returns zero only if
`except` is zero or all the
exceptions selected by `except` are successfully raised.
## `fesetenv`
int fesetenv(const fenv_t *penv);Copy to clipboard
The function attempts to restore the settings in the
[floating-point control](https://docs.qualcomm.com/doc/80-N3040-23/topic/c_fenv.html#floating-point%20control)
from `*penv`. It returns zero only if the settings
are successfully restored.
The settings must be determined by
`FE_DFL_ENV`
or by an earlier call to
`fegetenv` or
`feholdexcept`.
Otherwise, if `fetestexcept(
fegettrapenable())`
would be nonzero for the restored settings,
it is unspecified whether the function evaluation
results in a hardware trap and/or the raising of a
[signal](https://docs.qualcomm.com/doc/80-N3040-23/topic/c_signal.html#signals).
## `fesetexceptflag`
int fesetexceptflag(const fexcept_t *pflag, int except);Copy to clipboard
The function attempts to set
the exceptions selected by `except` in the
[floating-point status](https://docs.qualcomm.com/doc/80-N3040-23/topic/c_fenv.html#floating-point%20status) portion of the
[floating-point control](https://docs.qualcomm.com/doc/80-N3040-23/topic/c_fenv.html#floating-point%20control) to the
values of the corresponding bits selected by
`except & *pflag`.
It returns zero only if `except` is zero or all the
exceptions selected by `except & *pflag` are successfully set.
The value stored in `*pflag` must be determined by an earlier
call to `fegetexceptflag`,
without an intervening call to
`fesettrapenable`.
Otherwise, if `fegettrapenable() &
except & *pflag` is nonzero,
it is unspecified whether the function evaluation
results in a hardware trap and/or the raising of a
[signal](https://docs.qualcomm.com/doc/80-N3040-23/topic/c_signal.html#signals).
## `fesetround`
int fesetround(int mode);Copy to clipboard
The function sets the current
[rounding mode](https://docs.qualcomm.com/doc/80-N3040-23/topic/c_fenv.html#rounding%20mode) from `mode` in the
[floating-point control](https://docs.qualcomm.com/doc/80-N3040-23/topic/c_fenv.html#floating-point%20control).
An invalid value of `mode` leaves the rounding mode
unchanged. The function returns zero only if the rounding mode is
successfully set to `mode`.
## `fesettrapenable`
int fesettrapenable(fexcept_t enables); [non-standard]Copy to clipboard
The function sets the current
[floating-point enable mask](https://docs.qualcomm.com/doc/80-N3040-23/topic/c_fenv.html#floating-point%20enable%20mask) from
`enables`. An invalid value of `enables` leaves the
floating-point enable mask unchanged. The function returns zero only if the
floating-point enable mask is successfully set to `enables`.
If `fetestexcept(enables)` is nonzero,
it is unspecified whether `fesettrapenable(enables)`
results in a hardware trap and/or the raising of a
[signal](https://docs.qualcomm.com/doc/80-N3040-23/topic/c_signal.html#signals).
## `fetestexcept`
int fetestexcept(int except);Copy to clipboard
The function returns a nonzero value only if one or more of
the exceptions selected by `except` are set in the
[floating-point status](https://docs.qualcomm.com/doc/80-N3040-23/topic/c_fenv.html#floating-point%20status) portion of the
[floating-point control](https://docs.qualcomm.com/doc/80-N3040-23/topic/c_fenv.html#floating-point%20control).
## `feupdateenv`
void feupdateenv(const fenv_t *penv);Copy to clipboard
The function effectively executes:
int except = fetestexcept(FE_ALL_EXCEPT);
fesetenv(penv);
feraiseexcept(except);Copy to clipboard
Thus, it restores the settings in the
[floating-point control](https://docs.qualcomm.com/doc/80-N3040-23/topic/c_fenv.html#floating-point%20control) from
`*penv`, after first saving the exceptions selected by the current
[floating-point status](https://docs.qualcomm.com/doc/80-N3040-23/topic/c_fenv.html#floating-point%20status)
stored in the floating-point control. The function then
[raises](https://docs.qualcomm.com/doc/80-N3040-23/topic/c_fenv.html#raise%20a%20floating-point%20exception) the saved
exceptions. It returns zero only if the settings
are successfully restored.
The restored settings must be determined by
`FE_DFL_ENV`
or by an earlier call to
`fegetenv` or
`feholdexcept`.
Otherwise, it is unspecified whether the call `fesetenv(penv)`
results in a hardware trap and/or the raising of a
[signal](https://docs.qualcomm.com/doc/80-N3040-23/topic/c_signal.html#signals).
## `fexcept_t`
typedef i-type fexcept_t;Copy to clipboard
The type is an integer type `i-type`
that can represent the
[floating-point status](https://docs.qualcomm.com/doc/80-N3040-23/topic/c_fenv.html#floating-point%20status).
* * *
See also the
**[Table of Contents](https://docs.qualcomm.com/doc/80-N3040-23/topic/c_index.html#Table%20of%20Contents)** and the
**[Index](https://docs.qualcomm.com/doc/80-N3040-23/topic/_c_index.html)**.
*[Copyright](https://docs.qualcomm.com/doc/80-N3040-23/topic/c_crit_pjp.html) © 1992-2006by P.J. Plauger. All rights reserved.*
Last Published: Oct 06, 2026
Previous Topic
Next Topic