fenv.h(0P)
π fenv.h(0P) β the floating-point environment, but make it make sense π
okay so before anything else: hi bestie, welcome to a page from the POSIX Programmer's Manual. buckle up, we're talking about floating-point state and honestly? it's giving β¨systems girlieβ¨ energy.
π¨ PROLOG π¨
Real talk first: this manual page is part of the POSIX Programmer's Manual. The Linux implementation of this interface may differ β so like, go consult the corresponding Linux manual page for the actual Linux behavior β or honestly the interface may just not be implemented on Linux at all. π no cap.
β¨ NAME β¨
fenv.h β floating-point environment. short, sweet, iconic. π
πΈ SYNOPSIS πΈ
#include <fenv.h>
π DESCRIPTION π
So the functionality described on this reference page is aligned with the ISO C standard. And if there's any conflict between the requirements described here and the ISO C standard? That's unintentional. This volume of POSIX.1β2017 defers to the ISO C standard, periodt.
The <fenv.h> header shall define the following data types through typedef:
fenv_tβ Represents the entire floating-point environment. And what even is the floating-point environment, you ask? It refers collectively to any floating-point status flags and control modes supported by the implementation. it's the whole vibe, bestie. π«fexcept_tβ Represents the floating-point status flags collectively, including any status the implementation associates with the flags. Now let me explain the two concepts here because they're lowkey the whole plot:- A floating-point status flag is a system variable whose value is set (but never cleared) when a floating-point exception is raised. That raising happens as a side-effect of exceptional floating-point arithmetic, and it's there to provide auxiliary information. so it just. stays set. it does not un-set itself. π«
- A floating-point control mode is a system variable whose value may be set by the user to affect the subsequent behavior of floating-point arithmetic.
The <fenv.h> header shall define each of the following macros if and only if the implementation supports the floating-point exception by means of the floating-point functions feclearexcept(), fegetexceptflag(), feraiseexcept(), fesetexceptflag(), and fetestexcept(). The defined macros shall expand to integer constant expressions with values that are bitwise-distinct.
FE_DIVBYZERO FE_INEXACT FE_INVALID FE_OVERFLOW FE_UNDERFLOW
If the implementation supports the IEC 60559 Floating-Point option, all five macros shall be defined. Additional implementation-defined floating-point exceptions with macros beginning with FE_ and an uppercase letter may also be specified by the implementation. iykyk.
The <fenv.h> header shall define the macro FE_ALL_EXCEPT as the bitwise-inclusive OR of all floating-point exception macros defined by the implementation, if any. And if no such macros are defined? Then FE_ALL_EXCEPT shall be defined as zero. slay.
Next up: the <fenv.h> header shall define each of the following macros if and only if the implementation supports getting and setting the represented rounding direction by means of the fegetround() and fesetround() functions. The defined macros shall expand to integer constant expressions whose values are distinct non-negative values.
FE_DOWNWARD FE_TONEAREST FE_TOWARDZERO FE_UPWARD
If the implementation supports the IEC 60559 Floating-Point option, all four macros shall be defined. Additional implementation-defined rounding directions with macros beginning with FE_ and an uppercase letter may also be specified by the implementation. π
The <fenv.h> header shall also define the following macro, which represents the default floating-point environment β that is, the one installed at program startup β and has type pointer to const-qualified fenv_t. You can use it as an argument to the functions within the <fenv.h> header that manage the floating-point environment.
FE_DFL_ENV
The following shall be declared as functions and may also be defined as macros. Function prototypes shall be provided. π«Ά
int feclearexcept(int);
int fegetenv(fenv_t *);
int fegetexceptflag(fexcept_t *, int);
int fegetround(void);
int feholdexcept(fenv_t *);
int feraiseexcept(int);
int fesetenv(const fenv_t *);
int fesetexceptflag(const fexcept_t *, int);
int fesetround(int);
int fetestexcept(int);
int feupdateenv(const fenv_t *);
okay now the FENV_ACCESS pragma, and this part is dense so stay with me. The FENV_ACCESS pragma provides a means to inform the implementation when an application might access the floating-point environment to test floating-point status flags or run under non-default floating-point control modes. Here's the whole ruleset:
- The pragma shall occur either outside external declarations or preceding all explicit declarations and statements inside a compound statement.
- When outside external declarations, the pragma takes effect from its occurrence until another
FENV_ACCESSpragma is encountered, or until the end of the translation unit. - When inside a compound statement, the pragma takes effect from its occurrence until another
FENV_ACCESSpragma is encountered (including within a nested compound statement), or until the end of the compound statement. And at the end of a compound statement, the state for the pragma is restored to its condition just before the compound statement. π - If this pragma is used in any other context, the behavior is undefined. π©
- If part of an application tests floating-point status flags, sets floating-point control modes, or runs under non-default mode settings, but was translated with the state for the
FENV_ACCESSpragma off, the behavior is undefined. like, genuinely, don't. - The default state (on or off) for the pragma is implementation-defined.
- (And btw: when execution passes from a part of the application translated with
FENV_ACCESSoff to a part translated withFENV_ACCESSon, the state of the floating-point status flags is unspecified and the floating-point control modes have their default settings.)
The following sections are informative. π
π APPLICATION USAGE π
This header is designed to support the floating-point exception status flags and directed-rounding control modes required by the IEC 60559:1989 standard, plus other similar floating-point state information. It's also designed to facilitate code portability among all systems. very much a "works everywhere" moment. π
Certain application programming conventions support the intended model of use for the floating-point environment:
- A function call does not alter its caller's floating-point control modes, does not clear its caller's floating-point status flags, and does not depend on the state of its caller's floating-point status flags β unless the function is so documented. π
- A function call is assumed to require default floating-point control modes, unless its documentation promises otherwise.
- A function call is assumed to have the potential for raising floating-point exceptions, unless its documentation promises otherwise.
With these conventions, an application can safely assume default floating-point control modes (or just be completely unaware of them, honestly). The responsibilities associated with accessing the floating-point environment fall on the application that does so explicitly. main character responsibility energy. π
Even though the rounding direction macros may expand to constants corresponding to the values of FLT_ROUNDS, they are not required to do so. ngl that distinction matters.
For example:
#include <fenv.h>
void f(double x)
{
#pragma STDC FENV_ACCESS ON
void g(double);
void h(double);
/* ... */
g(x + 1);
h(x + 1);
/* ... */
}
If the function g() might depend on status flags set as a side-effect of the first x+1, or if the second x+1 might depend on control modes set as a side-effect of the call to function g(), then the application shall contain an appropriately placed invocation as follows:
#pragma STDC FENV_ACCESS ON
π§ RATIONALE π§
The fexcept_t Type
fexcept_t does not have to be an integer type. Its values must be obtained by a call to fegetexceptflag(), and cannot be created by logical operations from the exception macros. π«
An implementation might simply implement fexcept_t as an int and use the representations reflected by the exception macros β but it is not required to. Other representations might contain extra information about the exceptions. Like, fexcept_t might be a struct with a member for each exception (that might hold the address of the first or last floating-point instruction that caused that exception). The ISO/IEC 9899:1999 standard makes no claims about the internals of an fexcept_t, and so the user cannot inspect it. it's a mystery box, bestie, and you don't get to open it. π
Exception and Rounding Macros
Macros corresponding to unsupported modes and rounding directions are not defined by the implementation, and must not be defined by the application. An application might use #ifdef to test for this. π‘
π FUTURE DIRECTIONS π
None.
literally none. that's the whole section. we love a clean slate. β¨
π₯ SEE ALSO π₯
The System Interfaces volume of POSIX.1β2017, feclearexcept(), fegetenv(), fegetexceptflag(), fegetround(), feholdexcept(), feraiseexcept(), fetestexcept(), feupdateenv(). go read those, they're the supporting cast and they ate. π
π COPYRIGHT π
Portions of this text are reprinted and reproduced in electronic form from IEEE Std 1003.1-2017, Standard for Information Technology -- Portable Operating System Interface (POSIX), The Open Group Base Specifications Issue 7, 2018 Edition, Copyright (C) 2018 by the Institute of Electrical and Electronics Engineers, Inc and The Open Group. In the event of any discrepancy between this version and the original IEEE and The Open Group Standard, the original IEEE and The Open Group Standard is the referee document. The original Standard can be obtained online at http://www.opengroup.org/unix/online.html .
Any typographical or formatting errors that appear in this page are most likely to have been introduced during the conversion of the source files to man page format. To report such errors, see https://www.kernel.org/doc/man-pages/reporting_bugs.html .
and that's the whole page, bestie π«Ά this little love letter to floating-point state was brought to you by IEEE/The Open Group, 2017, filed under fenv.h(0P). go forth and set your rounding modes responsibly. ππ¦β¨
π¦ where this page comes from
- alpine 3.22: man-pages-posix 2017a-r0 Β· custom
- alpine 3.23: man-pages-posix 2017a-r0 Β· custom
- alpine 3.24: man-pages-posix 2017a-r0 Β· custom
- arch rolling: man-pages 6.19-1
- 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-posix 2017a
- opensuse-leap 16.0: man-pages-posix 2017a-160000.4.1 Β· SUSE-IEEE
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms