# **[](https://en.cppreference.com/w/c/header/fenv)** provides access to the floating-point environment: exception flags that record invalid operations, overflows, and underflows, plus rounding mode control. Essential for checking whether computations silently produced NaN or infinity. Use it to detect floating-point anomalies and test numerical edge cases portably. ## Example This example checks which floating-point exception flags are set after various computations. ```c // compile: gcc -o fenvexample fenvexample.c -lm // run: ./fenvexample // description: detect floating-point exceptions (invalid, overflow, etc.) #include #include #include int main() { volatile double x; feclearexcept(FE_ALL_EXCEPT); x = sqrt(-1.0); if (fetestexcept(FE_INVALID)) printf("sqrt(-1.0) set FE_INVALID\n"); feclearexcept(FE_ALL_EXCEPT); x = 1.0 / 0.0; if (fetestexcept(FE_DIVBYZERO)) printf("1.0/0.0 set FE_DIVBYZERO\n"); feclearexcept(FE_ALL_EXCEPT); x = 1e308 * 1e308; if (fetestexcept(FE_OVERFLOW)) printf("1e308*1e308 set FE_OVERFLOW\n"); return 0; } ``` ## Common functions and macros **Exception flags** (bit flags for fetestexcept, feclearexcept): - `FE_INEXACT`: result not representable - `FE_UNDERFLOW`: underflow to zero or subnormal - `FE_OVERFLOW`: result too large to represent - `FE_DIVBYZERO`: division by zero - `FE_INVALID`: invalid operation (sqrt(-1), NaN comparison) - `FE_ALL_EXCEPT`: all exception flags **Exception handling**: - `feclearexcept(excepts)`: clear specified flags - `fetestexcept(excepts)`: test which flags are set - `fegetexceptflag(&state, excepts)`: save flags to variable - `fesetexceptflag(&state, excepts)`: restore flags from variable **Rounding modes**: - `FE_DOWNWARD`, `FE_TONEAREST`, `FE_TOWARDZERO`, `FE_UPWARD` - `fegetround()`: query current rounding mode - `fesetround(mode)`: set rounding mode **Environment save/restore**: - `fegetenv(&env)`: save full FP environment - `fesetenv(&env)`: restore FP environment - `feupdateenv(&env)`: restore and re-raise current exceptions