fenv(3)
β¨ fenv(3) β¨ β floating-point rounding and exception handling π
okay bestie, sit DOWN, because this is the fenv(3) manual page and it's literally about floating-point rounding and exception handling, and it's giving "tiny functions, enormous power". no cap. π«Ά
πΈ NAME πΈ
feclearexcept, fegetexceptflag, feraiseexcept, fesetexceptflag, fetestexcept, fegetenv, fegetround, feholdexcept, fesetround, fesetenv, feupdateenv, feenableexcept, fedisableexcept, fegetexcept β floating-point rounding and exception handling.
likeβ¦ that's fourteen functions in one name line. main character energy fr fr π
π LIBRARY π
Math library (libm, -lm). so yeah, you're gonna need that -lm at link time, don't @ me. π
β¨ SYNOPSIS β¨
#include <fenv.h>
int feclearexcept(int excepts);
int fegetexceptflag(fexcept_t *flagp, int excepts);
int feraiseexcept(int excepts);
int fesetexceptflag(const fexcept_t *flagp, int excepts);
int fetestexcept(int excepts);
int fegetround(void);
int fesetround(int rounding_mode);
int fegetenv(fenv_t *envp);
int feholdexcept(fenv_t *envp);
int fesetenv(const fenv_t *envp);
int feupdateenv(const fenv_t *envp);
π§ DESCRIPTION π§
so these eleven functions were defined in C99, and they describe the handling of floating-point rounding and exceptions (overflow, zero-divide, etc.). eleven!! iconic. π
π₯ Exceptions
let's vibe-check each drama queen exception one by one:
- The divide-by-zero exception occurs when an operation on finite numbers produces infinity as exact answer. π
- The overflow exception occurs when a result has to be represented as a floating-point number, but has (much) larger absolute value than the largest (finite) floating-point number that is representable. it simply will not fit bestie π
- The underflow exception occurs when a result has to be represented as a floating-point number, but has smaller absolute value than the smallest positive normalized floating-point number (and would lose much accuracy when represented as a denormalized number). tiny little guy. so small. π₯Ί
- The inexact exception occurs when the rounded result of an operation is not equal to the infinite precision result. It may occur whenever overflow or underflow occurs. "close enough" is not a vibe it accepts β¨
- The invalid exception occurs when there is no well-defined result for an operation, as for
0/0orinfinity - infinityorsqrt(-1). it's giving undefined behaviour energy π
π Exception handling
Exceptions are represented in two ways:
- as a single bit (exception present/absent), and these bits correspond in some implementation-defined way with bit positions in an integer;
- as an opaque structure that may contain more information about the exception (perhaps the code address where it occurred).
Each of the macros FE_DIVBYZERO, FE_INEXACT, FE_INVALID, FE_OVERFLOW, FE_UNDERFLOW is defined when the implementation supports handling of the corresponding exception, and if so then defines the corresponding bit(s), so that one can call exception handling functions, for example, using the integer argument FE_OVERFLOW|FE_UNDERFLOW. Other exceptions may be supported. The macro FE_ALL_EXCEPT is the bitwise OR of all bits corresponding to supported exceptions. π
ok now the actual functions, bestie:
- The
feclearexcept()function clears the supported exceptions represented by the bits in its argument. - The
fegetexceptflag()function stores a representation of the state of the exception flags represented by the argument excepts in the opaque object*flagp. - The
feraiseexcept()function raises the supported exceptions represented by the bits in excepts. - The
fesetexceptflag()function sets the complete status for the exceptions represented by excepts to the value*flagp. This value must have been obtained by an earlier call offegetexceptflag()with a last argument that contained all bits in excepts. (read that twice, fr fr π) - The
fetestexcept()function returns a word in which the bits are set that were set in the argument excepts and for which the corresponding exception is currently set.
π Rounding mode
The rounding mode determines how the result of floating-point operations is treated when the result cannot be exactly represented in the significand. Various rounding modes may be provided: round to nearest (the default), round up (toward positive infinity), round down (toward negative infinity), and round toward zero. π―
Each of the macros FE_TONEAREST, FE_UPWARD, FE_DOWNWARD, and FE_TOWARDZERO is defined when the implementation supports getting and setting the corresponding rounding direction.
- The
fegetround()function returns the macro corresponding to the current rounding mode. - The
fesetround()function sets the rounding mode as specified by its argument and returns zero when it was successful. π
C99 and POSIX.1-2008 specify an identifier, FLT_ROUNDS, defined in <float.h>, which indicates the implementation-defined rounding behavior for floating-point addition. This identifier has one of the following values:
-1β The rounding mode is not determinable. π0β Rounding is toward 0.1β Rounding is toward nearest number.2β Rounding is toward positive infinity. β¬οΈ3β Rounding is toward negative infinity. β¬οΈ
Other values represent machine-dependent, nonstandard rounding modes.
The value of FLT_ROUNDS should reflect the current rounding mode as set by fesetround() (but see BUGS). π
π Floating-point environment
The entire floating-point environment, including control modes and status flags, can be handled as one opaque object, of type fenv_t. The default environment is denoted by FE_DFL_ENV (of type const fenv_t *). This is the environment setup at program start and it is defined by ISO C to have round to nearest, all exceptions cleared and a nonstop (continue on exceptions) mode. π§
- The
fegetenv()function saves the current floating-point environment in the object*envp. - The
feholdexcept()function does the same, then clears all exception flags, and sets a nonstop (continue on exceptions) mode, if available. It returns zero when successful. - The
fesetenv()function restores the floating-point environment from the object*envp. This object must be known to be valid, for example, the result of a call tofegetenv()orfeholdexcept()or equal toFE_DFL_ENV. This call does not raise exceptions. - The
feupdateenv()function installs the floating-point environment represented by the object*envp, except that currently raised exceptions are not cleared. After calling this function, the raised exceptions will be a bitwise OR of those previously set with those in*envp. As before, the object*envpmust be known to be valid. π«Ά
π― RETURN VALUE π―
These functions return zero on success and nonzero if an error occurred. that's it. that's the whole section. periodt. π
π‘οΈ ATTRIBUTES π‘οΈ
For an explanation of the terms used in this section, see attributes(7).
Thread safety for all of these β feclearexcept(), fegetexceptflag(), feraiseexcept(), fesetexceptflag(), fetestexcept(), fegetround(), fesetround(), fegetenv(), feholdexcept(), fesetenv(), feupdateenv(), feenableexcept(), fedisableexcept(), fegetexcept() β is MT-Safe. every single one of them. no exceptions. (lol. exceptions. π)
π STANDARDS π
C11, POSIX.1-2008, IEC 60559 (IEC 559:1989), ANSI/IEEE 854. certified, standardized, official. π
π°οΈ HISTORY π°οΈ
C99, POSIX.1-2001. glibc 2.1. a classic. a legend. π
π NOTES π
π§ glibc notes
If possible, the GNU C Library defines a macro FE_NOMASK_ENV which represents an environment where every exception raised causes a trap to occur. You can test for this macro using #ifdef. It is defined only if _GNU_SOURCE is defined. π¨
The C99 standard does not define a way to set individual bits in the floating-point mask, for example, to trap on specific flags. Since glibc 2.2, glibc supports the functions feenableexcept() and fedisableexcept() to set individual floating-point traps, and fegetexcept() to query the state.
#define _GNU_SOURCE /* See feature_test_macros(7) */
#include <fenv.h>
int feenableexcept(int excepts);
int fedisableexcept(int excepts);
int fegetexcept(void);
- The
feenableexcept()andfedisableexcept()functions enable (disable) traps for each of the exceptions represented by excepts and return the previous set of enabled exceptions when successful, and-1otherwise. - The
fegetexcept()function returns the set of all currently enabled exceptions. π
π BUGS π
C99 specifies that the value ofFLT_ROUNDSshould reflect changes to the current rounding mode, as set byfesetround(). Currently, this does not occur:FLT_ROUNDSalways has the value1. π
so yeah. it says it should, and it doesn't. the vibe check failed. don't build your whole thing on that assumption bestie π
π SEE ALSO π
that's the whole page bestie, you ate it, you slayed it π«Άπβ¨
this was fenv(3) from Linux man-pages 6.20, dated 2026-02-08. go forth and round responsibly, ily πππ¦
π¦ where this page comes from
- arch rolling: man-pages 6.20-1 Β· BSD-2-Clause AND BSD-3-Clause AND BSD-4.3TAHOE AND BSD-4-Clause-UC AND GPL-1.0-or-later AND GPL-2.0-only AND GPL-2.0-or-later AND GPL-3.0-or-later AND LGPL-3.0-linking-exception AND LGPL-3.0-or-later AND LicenseRef-Public-Domain AND LicenseRef-UltraPermissive AND Linux-man-pages-1-para AND Linux-man-pages-copyleft AND Linux-man-pages-copyleft-2-para AND Linux-man-pages-copyleft-var AND MIT AND Spencer-94
- nixpkgs unstable: man-pages 6.20
- π the page's license: GPL-2.0-or-later
- Copyright 2000, Andries E. Brouwer <aeb@cwi.nl>
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms