C Standard Library Fenv H
π
2026-06-23 | π C
### C Standard Library ``
`` is a header file in the C standard library used to control the floating-point environment.
`` was introduced in the C99 standard and provides functions for controlling and querying floating-point exceptions, rounding modes, and other floating-point states.
* * *
The main purposes of `` are:
* Detect and handle floating-point exceptions (such as division by zero, overflow, etc.).
* Control the rounding mode of floating-point operations (such as rounding toward zero, rounding to nearest, etc.).
* Query and modify floating-point status flags.
* * *
### 1γ**Floating-Point Exceptions**
Floating-point exceptions are special conditions that occur during floating-point operations, such as:
* **FE_DIVBYZERO**: Division by zero.
* **FE_INEXACT**: Inexact result.
* **FE_INVALID**: Invalid operation (such as square root of a negative number).
* **FE_OVERFLOW**: Overflow.
* **FE_UNDERFLOW**: Underflow.
These exceptions are represented by floating-point status flags and can be detected and handled through functions in ``.
* * *
### 2γ**Rounding Modes**
Rounding modes control how floating-point operation results are rounded. `` defines the following rounding modes:
* **FE_TONEAREST**: Round to the nearest value (default mode).
* **FE_DOWNWARD**: Round toward negative infinity.
* **FE_UPWARD**: Round toward positive infinity.
* **FE_TOWARDZERO**: Round toward zero.
* * *
### 3γ**Main Functions and Macros**
`` provides a set of functions and macros for manipulating the floating-point environment and handling floating-point exceptions.
#### Floating-Point Exception Handling
| Function/Macro | Description |
| --- | --- |
| `feclearexcept(int excepts)` | Clear the specified floating-point exception flags |
| `feraiseexcept(int excepts)` | Raise the specified floating-point exception |
| `fetestexcept(int excepts)` | Test whether the specified floating-point exception occurred |
| `fegetexceptflag(fexcept_t *flagp, int excepts)` | Get the floating-point exception flag status |
| `fesetexceptflag(const fexcept_t *flagp, int excepts)` | Set the floating-point exception flag status |
#### Rounding Mode Control
| Function/Macro | Description |
| --- | --- |
| `fegetround(void)` | Get the current rounding mode |
| `fesetround(int round)` | Set the current rounding mode |
#### Floating-Point Environment Control
| Function/Macro | Description |
| --- | --- |
| `fegetenv(fenv_t *envp)` | Save the current floating-point environment |
| `fesetenv(const fenv_t *envp)` | Restore the floating-point environment |
| `feholdexcept(fenv_t *envp)` | Save the current floating-point environment and clear exception flags |
| `feupdateenv(const fenv_t *envp)` | Restore the floating-point environment and raise exceptions |
* * *
### 4γ**Examples**
The following is an example using `` that demonstrates how to detect floating-point exceptions and control the rounding mode:
## Example
#include
#include
#include