xref: /freebsd/lib/msun/man/fenv.3 (revision 3157ba21)
1.\" Copyright (c) 2004 David Schultz <das@FreeBSD.org>
2.\" All rights reserved.
3.\"
4.\" Redistribution and use in source and binary forms, with or without
5.\" modification, are permitted provided that the following conditions
6.\" are met:
7.\" 1. Redistributions of source code must retain the above copyright
8.\"    notice, this list of conditions and the following disclaimer.
9.\" 2. Redistributions in binary form must reproduce the above copyright
10.\"    notice, this list of conditions and the following disclaimer in the
11.\"    documentation and/or other materials provided with the distribution.
12.\"
13.\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
14.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
17.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
23.\" SUCH DAMAGE.
24.\"
25.\" $FreeBSD$
26.\"
27.Dd March 16, 2005
28.Dt FENV 3
29.Os
30.Sh NAME
31.Nm feclearexcept ,
32.Nm fegetexceptflag ,
33.Nm feraiseexcept ,
34.Nm fesetexceptflag ,
35.Nm fetestexcept ,
36.Nm fegetround ,
37.Nm fesetround ,
38.Nm fegetenv ,
39.Nm feholdexcept ,
40.Nm fesetenv ,
41.Nm feupdateenv ,
42.Nm feenableexcept ,
43.Nm fedisableexcept ,
44.Nm fegetexcept
45.Nd floating-point environment control
46.Sh LIBRARY
47.Lb libm
48.Sh SYNOPSIS
49.In fenv.h
50.Fd "#pragma STDC FENV_ACCESS ON"
51.Ft int
52.Fn feclearexcept "int excepts"
53.Ft int
54.Fn fegetexceptflag "fexcept_t *flagp" "int excepts"
55.Ft int
56.Fn feraiseexcept "int excepts"
57.Ft int
58.Fn fesetexceptflag "const fexcept_t *flagp" "int excepts"
59.Ft int
60.Fn fetestexcept "int excepts"
61.Ft int
62.Fn fegetround void
63.Ft int
64.Fn fesetround "int round"
65.Ft int
66.Fn fegetenv "fenv_t *envp"
67.Ft int
68.Fn feholdexcept "fenv_t *envp"
69.Ft int
70.Fn fesetenv "const fenv_t *envp"
71.Ft int
72.Fn feupdateenv "const fenv_t *envp"
73.Ft int
74.Fn feenableexcept "int excepts"
75.Ft int
76.Fn fedisableexcept "int excepts"
77.Ft int
78.Fn fegetexcept void
79.Sh DESCRIPTION
80The
81.In fenv.h
82routines manipulate the floating-point environment,
83which includes the exception flags and rounding modes defined in
84.St -ieee754 .
85.Ss Exceptions
86Exception flags are set as side-effects of floating-point arithmetic
87operations and math library routines, and they remain set until
88explicitly cleared.
89The following macros expand to bit flags of type
90.Vt int
91representing the five standard floating-point exceptions.
92.Bl -tag -width ".Dv FE_DIVBYZERO"
93.It Dv FE_DIVBYZERO
94A divide-by-zero exception occurs when the program attempts to
95divide a finite non-zero number by zero.
96.It Dv FE_INEXACT
97An inexact exception is raised whenever there is a loss of precision
98due to rounding.
99.It Dv FE_INVALID
100Invalid operation exceptions occur when a program attempts to
101perform calculations for which there is no reasonable representable
102answer.
103For instance, subtraction of infinities, division of zero by zero,
104ordered comparison involving \*(Nas, and taking the square root of a
105negative number are all invalid operations.
106.It Dv FE_OVERFLOW
107An overflow exception occurs when the magnitude of the result of a
108computation is too large to fit in the destination type.
109.It Dv FE_UNDERFLOW
110Underflow occurs when the result of a computation is too close to zero
111to be represented as a non-zero value in the destination type.
112.El
113.Pp
114Additionally, the
115.Dv FE_ALL_EXCEPT
116macro expands to the bitwise OR of the above flags and any
117architecture-specific flags.
118Combinations of these flags are passed to the
119.Fn feclearexcept ,
120.Fn fegetexceptflag ,
121.Fn feraiseexcept ,
122.Fn fesetexceptflag ,
123and
124.Fn fetestexcept
125functions to clear, save, raise, restore, and examine the
126processor's floating-point exception flags, respectively.
127.Pp
128Exceptions may be
129.Em unmasked
130with
131.Fn feenableexcept
132and masked with
133.Fn fedisableexcept .
134Unmasked exceptions cause a trap when they are produced, and
135all exceptions are masked by default.
136The current mask can be tested with
137.Fn fegetexcept .
138.Ss Rounding Modes
139.St -ieee754
140specifies four rounding modes.
141These modes control the direction in which results are rounded
142from their exact values in order to fit them into binary
143floating-point variables.
144The four modes correspond with the following symbolic constants.
145.Bl -tag -width ".Dv FE_TOWARDZERO"
146.It Dv FE_TONEAREST
147Results are rounded to the closest representable value.
148If the exact result is exactly half way between two representable
149values, the value whose last binary digit is even (zero) is chosen.
150This is the default mode.
151.It Dv FE_DOWNWARD
152Results are rounded towards negative \*[If].
153.It Dv FE_UPWARD
154Results are rounded towards positive \*[If].
155.It Dv FE_TOWARDZERO
156Results are rounded towards zero.
157.El
158.Pp
159The
160.Fn fegetround
161and
162.Fn fesetround
163functions query and set the rounding mode.
164.Ss Environment Control
165The
166.Fn fegetenv
167and
168.Fn fesetenv
169functions save and restore the floating-point environment,
170which includes exception flags, the current exception mask,
171the rounding mode, and possibly other implementation-specific
172state.
173The
174.Fn feholdexcept
175function behaves like
176.Fn fegetenv ,
177but with the additional effect of clearing the exception flags and
178installing a
179.Em non-stop
180mode.
181In non-stop mode, floating-point operations will set exception flags
182as usual, but no
183.Dv SIGFPE
184signals will be generated as a result.
185Non-stop mode is the default, but it may be altered by
186non-standard mechanisms.
187.\" XXX Mention fe[gs]etmask() here after the interface is finalized
188.\" XXX and ready to be officially documented.
189The
190.Fn feupdateenv
191function restores a saved environment similarly to
192.Fn fesetenv ,
193but it also re-raises any floating-point exceptions from the old
194environment.
195.Pp
196The macro
197.Dv FE_DFL_ENV
198expands to a pointer to the default environment.
199.Sh EXAMPLES
200The following routine computes the square root function.
201It explicitly raises an invalid exception on appropriate inputs using
202.Fn feraiseexcept .
203It also defers inexact exceptions while it computes intermediate
204values, and then it allows an inexact exception to be raised only if
205the final answer is inexact.
206.Bd -literal -offset indent
207#pragma STDC FENV_ACCESS ON
208double sqrt(double n) {
209	double x = 1.0;
210	fenv_t env;
211
212	if (isnan(n) || n < 0.0) {
213		feraiseexcept(FE_INVALID);
214		return (NAN);
215	}
216	if (isinf(n) || n == 0.0)
217		return (n);
218	feholdexcept(&env);
219	while (fabs((x * x) - n) > DBL_EPSILON * 2 * x)
220		x = (x / 2) + (n / (2 * x));
221	if (x * x == n)
222		feclearexcept(FE_INEXACT);
223	feupdateenv(&env);
224	return (x);
225}
226.Ed
227.Sh SEE ALSO
228.Xr cc 1 ,
229.Xr feclearexcept 3 ,
230.Xr fedisableexcept 3 ,
231.Xr feenableexcept 3 ,
232.Xr fegetenv 3 ,
233.Xr fegetexcept 3 ,
234.Xr fegetexceptflag 3 ,
235.Xr fegetround 3 ,
236.Xr feholdexcept 3 ,
237.Xr feraiseexcept 3 ,
238.Xr fesetenv 3 ,
239.Xr fesetexceptflag 3 ,
240.Xr fesetround 3 ,
241.Xr fetestexcept 3 ,
242.Xr feupdateenv 3 ,
243.Xr fpgetprec 3 ,
244.Xr fpsetprec 3
245.Sh STANDARDS
246Except as noted below,
247.In fenv.h
248conforms to
249.St -isoC-99 .
250The
251.Fn feenableexcept ,
252.Fn fedisableexcept ,
253and
254.Fn fegetexcept
255routines are extensions.
256.Sh HISTORY
257The
258.In fenv.h
259header first appeared in
260.Fx 5.3 .
261It supersedes the non-standard routines defined in
262.In ieeefp.h
263and documented in
264.Xr fpgetround 3 .
265.Sh CAVEATS
266The FENV_ACCESS pragma can be enabled with
267.Dl "#pragma STDC FENV_ACCESS ON"
268and disabled with the
269.Dl "#pragma STDC FENV_ACCESS OFF"
270directive.
271This lexically-scoped annotation tells the compiler that the program
272may access the floating-point environment, so optimizations that would
273violate strict IEEE-754 semantics are disabled.
274If execution reaches a block of code for which
275.Dv FENV_ACCESS
276is off, the floating-point environment will become undefined.
277.Sh BUGS
278The
279.Dv FENV_ACCESS
280pragma is unimplemented in the system compiler.
281However, non-constant expressions generally produce the correct
282side-effects at low optimization levels.
283