C Reference ยท ctype.h, math.h and others
C fenv.h: the floating-point environment
Reference for fenv.h: read and set the rounding mode, test and clear floating-point exception flags, and save or restore the whole environment.
<fenv.h> gives you access to two pieces of state in the floating-point unit: the exception flags and the rounding mode. When an operation divides by zero, overflows, or produces an invalid result, the hardware sets a sticky flag instead of stopping the program. You can read and clear those flags with fetestexcept() and feclearexcept(). The rounding mode decides how an inexact result is rounded, and you can change it with fesetround().
Most programs never touch this header. It matters for numerical libraries, interval arithmetic, and tests that want to know whether a computation was exact.
#include <fenv.h> feclearexcept(FE_ALL_EXCEPT); double r = a / b; if (fetestexcept(FE_DIVBYZERO)) { /* b was zero */ }
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.
Syntax
int feclearexcept(int excepts); int fetestexcept(int excepts); int feraiseexcept(int excepts); int fegetexceptflag(fexcept_t *flagp, int excepts); int fesetexceptflag(const fexcept_t *flagp, int excepts); int fegetround(void); int fesetround(int round); int fegetenv(fenv_t *envp); int fesetenv(const fenv_t *envp); int feholdexcept(fenv_t *envp); int feupdateenv(const fenv_t *envp);
| Name | Description |
|---|---|
excepts |
A bitwise OR of exception macros. FE_ALL_EXCEPT means all supported ones. |
round |
One rounding-mode macro. |
envp |
Pointer to an fenv_t object that holds the full environment. |
Exception macros (each is defined only if the hardware supports it): FE_DIVBYZERO, FE_INEXACT, FE_INVALID, FE_OVERFLOW, FE_UNDERFLOW.
Rounding macros (same rule): FE_TONEAREST (the default, ties to even), FE_UPWARD, FE_DOWNWARD, FE_TOWARDZERO. The macro FE_DFL_ENV is a pointer to the default environment.
Returns: fetestexcept() returns the bitwise OR of the flags that are currently set among excepts. fegetround() returns the current mode, or a negative value if it cannot be determined. All other functions return 0 on success and nonzero on failure.
Errors / undefined behavior: passing a value that is not a valid rounding macro to fesetround() fails and leaves the mode unchanged. Calling these functions has no reliable effect when the compiler is free to ignore them (see the notes).
Usage notes
- Flags are sticky. Once an operation sets
FE_OVERFLOW, the flag stays set until you clear it. Callfeclearexcept(FE_ALL_EXCEPT)before the code you want to examine. FE_INEXACTis the common one. Most operations on non-trivial numbers (1.0 / 3.0) are inexact, so do not treat it as an error.FE_DIVBYZERO,FE_INVALIDandFE_OVERFLOWusually indicate real trouble.- Tell the compiler you use the environment. The standard way is
#pragma STDC FENV_ACCESS ON, but GCC ignores that pragma (and warns about it); with GCC use-frounding-mathand keep operandsvolatileor otherwise opaque, as the examples do. Without that, constant folding and reordering can move an operation acrossfesetround()orfetestexcept(). - The mode applies to the current thread on mainstream platforms. Each new thread starts with the default environment on glibc.
printfobeys the rounding mode too, so digits printed while a non-default mode is active can differ in the last place. Restore the mode before printing, as the second example does.- Always restore the mode. A library that changes the rounding mode and does not put it back breaks every caller after it.
fegetenv()/fesetenv()is the safe pair;feholdexcept()saves the environment and clears the flags in one call, andfeupdateenv()restores it while re-raising anything raised in between. - Exceptions do not trap by default. Turning them into
SIGFPEis a platform extension (feenableexcept()in glibc), not part of ISO C. - Math functions and
errno: many math functions also report errors through the flags;math_errhandlingsays which mechanisms are in use (see Math constants). - Standard version: C99. Linking with
-lmis not needed on the runner.