1 .\" Copyright (c) 2008, Linux Foundation, written by Michael Kerrisk
2 .\" <mtk.manpages@gmail.com>
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.
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.
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
21 .\" Formatted or processed versions of this manual, if unaccompanied by
22 .\" the source, must acknowledge the copyright and authors of this work.
24 .TH MATH_ERROR 7 2008-08-11 "Linux" "Linux Programmer's Manual"
26 math_error \- detecting errors from mathematical functions
35 most library functions indicate this fact by returning a special value
37 Because they typically return a floating-point number,
38 the mathematical functions declared in
40 indicate an error using other mechanisms.
41 There are two error-reporting mechanisms:
44 the newer one uses the floating-point exception mechanism (the use of
52 A portable program that needs to check for an error from a mathematical
55 to zero, and make the following call
59 feclearexcept(FE_ALL_EXCEPT);
63 before calling a mathematical function.
65 Upon return from the mathematical function, if
67 is non-zero, or the following call (see
73 fetestexcept(FE_INVALID | FE_DIVBYZERO | FE_OVERFLOW |
80 .\" FE_INVALID = 0x01,
81 .\" __FE_DENORM = 0x02,
82 .\" FE_DIVBYZERO = 0x04,
83 .\" FE_OVERFLOW = 0x08,
84 .\" FE_UNDERFLOW = 0x10,
87 then an error occurred in the mathematical function.
89 The error conditions that can occur for mathematical functions
94 occurs when a mathematical function is supplied with an argument whose
95 value falls outside the domain for which the function
96 is defined (e.g., giving a negative argument to
98 When a domain error occurs,
99 math functions commonly return a NaN
100 (though some functions return a different value in this case);
106 floating-point exception is raised.
110 occurs when the mathematical result of a function is an exact infinity
111 (e.g., the logarithm of 0 is negative infinity).
112 When a pole error occurs,
113 the function returns the (signed) value
118 depending on whether the function result type is
123 The sign of the result is that which is mathematically correct for
128 and a "divide-by-zero"
130 floating-point exception is raised.
134 occurs when the magnitude of the function result means that it
135 cannot be represented in the result type of the function.
136 The return value of the function depends on whether the range error
137 was an overflow or an underflow.
141 if the result is finite,
142 but is too large to represented in the result type.
143 When an overflow occurs,
144 the function returns the value
149 depending on whether the function result type is
159 floating-point exception is raised.
163 if the result is too small to be represented in the result type.
164 If an underflow occurs,
165 a mathematical function typically returns 0.0
166 (C99 says a function shall return "an implementation-defined value
167 whose magnitude is no greater than the smallest normalized
168 positive number in the specified type").
169 .\" FIXME(mtk) POSIX.1 says "may" for the following two cases; need to
170 .\" investigate this further for specific functions.
176 floating-point exception may be raised.
178 Some functions deliver a range error if the supplied argument value,
179 or the correct function result, would be
181 A subnormal value is one that is non-zero,
182 but with a magnitude that is so small that
183 it can't be presented in normalized form
184 (i.e., with a 1 in the most significant bit of the significand).
185 The representation of a subnormal number will contain one
186 or more leading zeros in the significand.
190 identifier specified by C99 and POSIX.1-2001 is not supported by glibc.
191 .\" See CONFORMANCE in the glibc 2.8 (and earlier) source.
192 This identifer is supposed to indicate which of the two
193 error-notification mechanisms
195 exceptions retrievable via
196 .BR fettestexcept (3))
198 The standards require that at least one be in use,
199 but permit both to be available.
200 The current (version 2.8) situation under glibc is messy.
201 Most (but not all) functions raise exceptions on errors.
206 but don't raise an exception.
207 A very few functions do neither.
208 See the individual manual pages for details.
210 To avoid the complexities of using
215 it is often advised that one should instead check for bad argument
216 values before each call.
217 .\" http://www.securecoding.cert.org/confluence/display/seccode/FLP32-C.+Prevent+or+detect+domain+and+range+errors+in+math+functions
218 For example, the following code ensures that
220 argument is not a NaN and is not zero (a pole error) or
221 less than zero (a domain error):
227 if (isnan(x) || islessequal(x, 0)) {
228 /* Deal with NaN / pole error / domain error */
235 The discussion on this page does not apply to the complex
236 mathematical functions (i.e., those declared by
238 which in general are not required to return errors by C99
244 option causes the executable to employ implementations of some
245 mathematical functions that are faster than the standard
246 implementations, but do not set
253 .IR "-fno-math-errno" .)
254 An error can still be tested for using
255 .BR fetestexcept (3).