1fenv.h(0P) POSIX Programmer's Manual fenv.h(0P)
2
3
4
6 This manual page is part of the POSIX Programmer's Manual. The Linux
7 implementation of this interface may differ (consult the corresponding
8 Linux manual page for details of Linux behavior), or the interface may
9 not be implemented on Linux.
10
12 fenv.h — floating-point environment
13
15 #include <fenv.h>
16
18 The functionality described on this reference page is aligned with the
19 ISO C standard. Any conflict between the requirements described here
20 and the ISO C standard is unintentional. This volume of POSIX.1‐2017
21 defers to the ISO C standard.
22
23 The <fenv.h> header shall define the following data types through type‐
24 def:
25
26 fenv_t Represents the entire floating-point environment. The float‐
27 ing-point environment refers collectively to any floating-
28 point status flags and control modes supported by the imple‐
29 mentation.
30
31 fexcept_t Represents the floating-point status flags collectively,
32 including any status the implementation associates with the
33 flags. A floating-point status flag is a system variable
34 whose value is set (but never cleared) when a floating-point
35 exception is raised, which occurs as a side-effect of excep‐
36 tional floating-point arithmetic to provide auxiliary infor‐
37 mation. A floating-point control mode is a system variable
38 whose value may be set by the user to affect the subsequent
39 behavior of floating-point arithmetic.
40
41 The <fenv.h> header shall define each of the following macros if and
42 only if the implementation supports the floating-point exception by
43 means of the floating-point functions feclearexcept(), fegetexcept‐
44 flag(), feraiseexcept(), fesetexceptflag(), and fetestexcept(). The
45 defined macros shall expand to integer constant expressions with values
46 that are bitwise-distinct.
47
48 FE_DIVBYZERO FE_INEXACT FE_INVALID FE_OVERFLOW FE_UNDERFLOW
49
50 If the implementation supports the IEC 60559 Floating-Point option, all
51 five macros shall be defined. Additional implementation-defined float‐
52 ing-point exceptions with macros beginning with FE_ and an uppercase
53 letter may also be specified by the implementation.
54
55 The <fenv.h> header shall define the macro FE_ALL_EXCEPT as the bit‐
56 wise-inclusive OR of all floating-point exception macros defined by the
57 implementation, if any. If no such macros are defined, then the macro
58 FE_ALL_EXCEPT shall be defined as zero.
59
60 The <fenv.h> header shall define each of the following macros if and
61 only if the implementation supports getting and setting the represented
62 rounding direction by means of the fegetround() and fesetround() func‐
63 tions. The defined macros shall expand to integer constant expressions
64 whose values are distinct non-negative values.
65
66 FE_DOWNWARD FE_TONEAREST FE_TOWARDZERO FE_UPWARD
67
68 If the implementation supports the IEC 60559 Floating-Point option, all
69 four macros shall be defined. Additional implementation-defined round‐
70 ing directions with macros beginning with FE_ and an uppercase letter
71 may also be specified by the implementation.
72
73 The <fenv.h> header shall define the following macro, which represents
74 the default floating-point environment (that is, the one installed at
75 program startup) and has type pointer to const-qualified fenv_t. It
76 can be used as an argument to the functions within the <fenv.h> header
77 that manage the floating-point environment.
78
79 FE_DFL_ENV
80
81 The following shall be declared as functions and may also be defined as
82 macros. Function prototypes shall be provided.
83
84
85 int feclearexcept(int);
86 int fegetenv(fenv_t *);
87 int fegetexceptflag(fexcept_t *, int);
88 int fegetround(void);
89 int feholdexcept(fenv_t *);
90 int feraiseexcept(int);
91 int fesetenv(const fenv_t *);
92 int fesetexceptflag(const fexcept_t *, int);
93 int fesetround(int);
94 int fetestexcept(int);
95 int feupdateenv(const fenv_t *);
96
97 The FENV_ACCESS pragma provides a means to inform the implementation
98 when an application might access the floating-point environment to test
99 floating-point status flags or run under non-default floating-point
100 control modes. The pragma shall occur either outside external declara‐
101 tions or preceding all explicit declarations and statements inside a
102 compound statement. When outside external declarations, the pragma
103 takes effect from its occurrence until another FENV_ACCESS pragma is
104 encountered, or until the end of the translation unit. When inside a
105 compound statement, the pragma takes effect from its occurrence until
106 another FENV_ACCESS pragma is encountered (including within a nested
107 compound statement), or until the end of the compound statement; at the
108 end of a compound statement the state for the pragma is restored to its
109 condition just before the compound statement. If this pragma is used in
110 any other context, the behavior is undefined. If part of an application
111 tests floating-point status flags, sets floating-point control modes,
112 or runs under non-default mode settings, but was translated with the
113 state for the FENV_ACCESS pragma off, the behavior is undefined. The
114 default state (on or off) for the pragma is implementation-defined.
115 (When execution passes from a part of the application translated with
116 FENV_ACCESS off to a part translated with FENV_ACCESS on, the state of
117 the floating-point status flags is unspecified and the floating-point
118 control modes have their default settings.)
119
120 The following sections are informative.
121
123 This header is designed to support the floating-point exception status
124 flags and directed-rounding control modes required by the
125 IEC 60559:1989 standard, and other similar floating-point state infor‐
126 mation. Also it is designed to facilitate code portability among all
127 systems.
128
129 Certain application programming conventions support the intended model
130 of use for the floating-point environment:
131
132 * A function call does not alter its caller's floating-point control
133 modes, clear its caller's floating-point status flags, nor depend
134 on the state of its caller's floating-point status flags unless the
135 function is so documented.
136
137 * A function call is assumed to require default floating-point con‐
138 trol modes, unless its documentation promises otherwise.
139
140 * A function call is assumed to have the potential for raising float‐
141 ing-point exceptions, unless its documentation promises otherwise.
142
143 With these conventions, an application can safely assume default float‐
144 ing-point control modes (or be unaware of them). The responsibilities
145 associated with accessing the floating-point environment fall on the
146 application that does so explicitly.
147
148 Even though the rounding direction macros may expand to constants cor‐
149 responding to the values of FLT_ROUNDS, they are not required to do so.
150
151 For example:
152
153
154 #include <fenv.h>
155 void f(double x)
156 {
157 #pragma STDC FENV_ACCESS ON
158 void g(double);
159 void h(double);
160 /* ... */
161 g(x + 1);
162 h(x + 1);
163 /* ... */
164 }
165
166 If the function g() might depend on status flags set as a side-effect
167 of the first x+1, or if the second x+1 might depend on control modes
168 set as a side-effect of the call to function g(), then the application
169 shall contain an appropriately placed invocation as follows:
170
171
172 #pragma STDC FENV_ACCESS ON
173
175 The fexcept_t Type
176 fexcept_t does not have to be an integer type. Its values must be
177 obtained by a call to fegetexceptflag(), and cannot be created by logi‐
178 cal operations from the exception macros. An implementation might sim‐
179 ply implement fexcept_t as an int and use the representations reflected
180 by the exception macros, but is not required to; other representations
181 might contain extra information about the exceptions. fexcept_t might
182 be a struct with a member for each exception (that might hold the
183 address of the first or last floating-point instruction that caused
184 that exception). The ISO/IEC 9899:1999 standard makes no claims about
185 the internals of an fexcept_t, and so the user cannot inspect it.
186
187 Exception and Rounding Macros
188 Macros corresponding to unsupported modes and rounding directions are
189 not defined by the implementation and must not be defined by the appli‐
190 cation. An application might use #ifdef to test for this.
191
193 None.
194
196 The System Interfaces volume of POSIX.1‐2017, feclearexcept(),
197 fegetenv(), fegetexceptflag(), fegetround(), feholdexcept(), feraiseex‐
198 cept(), fetestexcept(), feupdateenv()
199
201 Portions of this text are reprinted and reproduced in electronic form
202 from IEEE Std 1003.1-2017, Standard for Information Technology -- Por‐
203 table Operating System Interface (POSIX), The Open Group Base Specifi‐
204 cations Issue 7, 2018 Edition, Copyright (C) 2018 by the Institute of
205 Electrical and Electronics Engineers, Inc and The Open Group. In the
206 event of any discrepancy between this version and the original IEEE and
207 The Open Group Standard, the original IEEE and The Open Group Standard
208 is the referee document. The original Standard can be obtained online
209 at http://www.opengroup.org/unix/online.html .
210
211 Any typographical or formatting errors that appear in this page are
212 most likely to have been introduced during the conversion of the source
213 files to man page format. To report such errors, see https://www.ker‐
214 nel.org/doc/man-pages/reporting_bugs.html .
215
216
217
218IEEE/The Open Group 2017 fenv.h(0P)