]> git.ipfire.org Git - thirdparty/man-pages.git/blob - man7/math_error.7
NOTES: Summarize the state of glibc support for exceptions
[thirdparty/man-pages.git] / man7 / math_error.7
1 .\" Copyright (c) 2008, Linux Foundation, written by Michael Kerrisk
2 .\" <mtk.manpages@gmail.com>
3 .\"
4 .\" Permission is granted to make and distribute verbatim copies of this
5 .\" manual provided the copyright notice and this permission notice are
6 .\" preserved on all copies.
7 .\"
8 .\" Permission is granted to copy and distribute modified versions of this
9 .\" manual under the conditions for verbatim copying, provided that the
10 .\" entire resulting derived work is distributed under the terms of a
11 .\" permission notice identical to this one.
12 .\"
13 .\" Since the Linux kernel and libraries are constantly changing, this
14 .\" manual page may be incorrect or out-of-date. The author(s) assume no
15 .\" responsibility for errors or omissions, or for damages resulting from
16 .\" the use of the information contained herein. The author(s) may not
17 .\" have taken the same level of care in the production of this manual,
18 .\" which is licensed free of charge, as they might when working
19 .\" professionally.
20 .\"
21 .\" Formatted or processed versions of this manual, if unaccompanied by
22 .\" the source, must acknowledge the copyright and authors of this work.
23 .\"
24 .TH MATH_ERROR 7 2008-07-30 "Linux" "Linux Programmer's Manual"
25 .SH NAME
26 math_error \- detecting errors from mathematical functions
27 .SH SYNOPSIS
28 .nf
29 .B #include <math.h>
30 .B #include <errno.h>
31 .B #include <fenv.h>
32 .fi
33 .SH DESCRIPTION
34 On error, many of the mathematical functions declared in
35 .IR <math.h>
36 return a NaN (not a number).
37 However, rather than looking at the return value
38 (which is not always possible)
39 one can also check whether an error was signaled.
40 There are two signaling mechanisms:
41 the older one sets
42 .IR errno ;
43 the newer one uses the floating-point exception mechanism (the use of
44 .BR feclearexcept (3)
45 and
46 .BR fetestexcept (3),
47 as outlined below)
48 described in
49 .BR fenv (3).
50
51 A portable program that needs to check for an error from a mathematical
52 function should set
53 .I errno
54 to zero, and make the following call
55 .in +4n
56 .nf
57
58 feclearexcept(FE_ALL_EXCEPT);
59
60 .fi
61 .in
62 before calling a mathematical function.
63
64 Upon return from the mathematical function, if
65 .I errno
66 is non-zero, or the following call (see
67 .BR fenv (3))
68 returns non-zero
69 .in +4n
70 .nf
71
72 fetestexcept(FE_INVALID | FE_DIVBYZERO | FE_OVERFLOW |
73 FE_UNDERFLOW);
74
75 .fi
76 .in
77 .\" enum
78 .\" {
79 .\" FE_INVALID = 0x01,
80 .\" __FE_DENORM = 0x02,
81 .\" FE_DIVBYZERO = 0x04,
82 .\" FE_OVERFLOW = 0x08,
83 .\" FE_UNDERFLOW = 0x10,
84 .\" FE_INEXACT = 0x20
85 .\" };
86 then an error occurred in the mathematical function.
87
88 The error conditions that can occur for mathematical functions
89 are described below.
90 .SS Domain Error
91 A
92 .I domain error
93 occurs when a mathematical function is supplied with an argument whose
94 value falls outside the domain for which the function
95 is defined (e.g., giving a negative argument to
96 .BR log (3)).
97 When a domain error occurs,
98 .I errno
99 is set to
100 .BR EDOM ,
101 and an "invalid"
102 .RB ( FE_INVALID )
103 floating-point exception is raised.
104 .SS Pole Error
105 A
106 .I pole error
107 occurs when the mathematical result of a function is an exact infinity
108 (e.g., the logarithm of 0 is negative infinity).
109 When a pole error occurs,
110 the function returns the (signed) value
111 .BR HUGE_VAL ,
112 .BR HUGE_VALF ,
113 or
114 .BR HUGE_VALL ,
115 depending on whether the function result type is
116 .IR double ,
117 .IR float ,
118 or
119 .IR "long double" .
120 The sign of the result is that which is mathematically correct for
121 the function.
122 .I errno
123 is set to
124 .BR ERANGE ,
125 and a "divide-by-zero"
126 .RB ( FE_DIVBYZERO )
127 floating-point exception is raised.
128 .SS Range Error
129 A
130 .I range error
131 occurs when the magnitude of the function result means that it
132 cannot be represented in the result type of the function.
133 The return value of the function depends on whether the range error
134 was an overflow or an underflow.
135
136 A floating result
137 .I overflows
138 if the result is finite,
139 but is too large to represented in the result type.
140 When an overflow occurs,
141 the function returns the value
142 .BR HUGE_VAL ,
143 .BR HUGE_VALF ,
144 or
145 .BR HUGE_VALL ,
146 depending on whether the function result type is
147 .IR double ,
148 .IR float ,
149 or
150 .IR "long double" .
151 .I errno
152 is set to
153 .BR ERANGE ,
154 and an "overflow"
155 .RB ( FE_OVERFLOW )
156 floating-point exception is raised.
157
158 A floating result
159 .I underflows
160 if the result is too small to be represented in the result type.
161 If an underflow occurs,
162 a mathematical function typically returns 0.0
163 (C99 says a function shall return "an implementation-defined value
164 whose magnitude is no greater than the smallest normalized
165 positive number in the specified type").
166 .\" FIXME(mtk) POSIX.1 says "may" for the following two cases; need to
167 .\" investigate this further for specific functions.
168 .I errno
169 may be set to
170 .BR ERANGE ,
171 and an "overflow"
172 .RB ( FE_UNDERFLOW )
173 floating-point exception may be raised.
174
175 Some functions deliver a range error if the supplied argument value,
176 or the correct function result, would be
177 .IR subnormal .
178 A subnormal value is one that is non-zero,
179 but with a magnitude that is so small that
180 it can't be presented in normalized form
181 (i.e., with a 1 in the most significant bit of the significand).
182 The representation of a subnormal number will contain one
183 or more leading zeros in the significand.
184 .SH NOTES
185 The
186 .I math_errhandling
187 identifier specified by C99 and POSIX.1-2001 is not supported by glibc.
188 .\" See CONFORMANCE in the glibc 2.8 (and earlier) source.
189 This identifer is supposed to indicate which of the two
190 error-notification mechanisms
191 .RI ( errno ,
192 exceptions retrievable via
193 .BR fettestexcept (3))
194 is in use.
195 The standards require that at least one be in use,
196 but permit both to be available.
197 The current (version 2.8) situation under glibc is messy.
198 Most (but not all) functions raise exceptions on errors.
199 Some also set
200 .IR errno .
201 A few functions set
202 .IR errno ,
203 but don't raise an exception.
204 A very few functions do neither.
205 See the individual manual pages for details.
206
207 To avoid the complexities of using
208 .I errno
209 and
210 .BR fetestexcept (3)
211 for error checking,
212 it is often advised that one should instead check for bad argument
213 values before each call.
214 .\" http://www.securecoding.cert.org/confluence/display/seccode/FLP32-C.+Prevent+or+detect+domain+and+range+errors+in+math+functions
215 For example, the following code ensures that
216 .BR log (3)'s
217 argument is not a NaN and is not zero (a pole error) or
218 less than zero (a domain error):
219 .in +4n
220 .nf
221
222 double x, r;
223
224 if (isnan(x) || islessequal(x, 0)) {
225 /* Deal with NaN / pole error / domain error */
226 }
227
228 r = log(x);
229
230 .fi
231 .in
232 The discussion on this page does not apply to the complex
233 mathematical functions (i.e., those declared by
234 .IR <complex.h> ),
235 which in general are not required to return errors by C99
236 and POSIX.1-2001.
237
238 The
239 .BR gcc (1)
240 .I "-fno-math-errno"
241 option causes the executable to employ implementations of some
242 mathematical functions that are faster than the standard
243 implementations, but do not set
244 .I errno
245 on error.
246 (The
247 .BR gcc (1)
248 .I "-ffast-math"
249 option also enables
250 .IR "-fno-math-errno" .)
251 An error can still be tested for using
252 .BR fetestexcept (3).
253 .SH SEE ALSO
254 .BR gcc (1),
255 .BR errno (3),
256 .BR fenv (3),
257 .BR fpclassify (3),
258 .BR INFINITY (3),
259 .BR isgreater (3),
260 .BR matherr (3),
261 .BR nan (3)
262 .br
263 .I "info libc"