1 .\" Copyright (c) 2008, Linux Foundation, written by Michael Kerrisk
2 .\" <mtk.manpages@gmail.com>
4 .\" SPDX-License-Identifier: Linux-man-pages-copyleft
6 .TH math_error 7 (date) "Linux man-pages (unreleased)"
8 math_error \- detecting errors from mathematical functions
17 most library functions indicate this fact by returning a special value
19 Because they typically return a floating-point number,
20 the mathematical functions declared in
22 indicate an error using other mechanisms.
23 There are two error-reporting mechanisms:
26 the newer one uses the floating-point exception mechanism (the use of
34 A portable program that needs to check for an error from a mathematical
37 to zero, and make the following call
41 feclearexcept(FE_ALL_EXCEPT);
45 before calling a mathematical function.
47 Upon return from the mathematical function, if
49 is nonzero, or the following call (see
55 fetestexcept(FE_INVALID | FE_DIVBYZERO | FE_OVERFLOW |
62 .\" FE_INVALID = 0x01,
63 .\" __FE_DENORM = 0x02,
64 .\" FE_DIVBYZERO = 0x04,
65 .\" FE_OVERFLOW = 0x08,
66 .\" FE_UNDERFLOW = 0x10,
69 then an error occurred in the mathematical function.
71 The error conditions that can occur for mathematical functions
76 occurs when a mathematical function is supplied with an argument whose
77 value falls outside the domain for which the function
78 is defined (e.g., giving a negative argument to
80 When a domain error occurs,
81 math functions commonly return a NaN
82 (though some functions return a different value in this case);
88 floating-point exception is raised.
92 occurs when the mathematical result of a function is an exact infinity
93 (e.g., the logarithm of 0 is negative infinity).
94 When a pole error occurs,
95 the function returns the (signed) value
100 depending on whether the function result type is
105 The sign of the result is that which is mathematically correct for
110 and a "divide-by-zero"
112 floating-point exception is raised.
116 occurs when the magnitude of the function result means that it
117 cannot be represented in the result type of the function.
118 The return value of the function depends on whether the range error
119 was an overflow or an underflow.
123 if the result is finite,
124 but is too large to represented in the result type.
125 When an overflow occurs,
126 the function returns the value
131 depending on whether the function result type is
141 floating-point exception is raised.
145 if the result is too small to be represented in the result type.
146 If an underflow occurs,
147 a mathematical function typically returns 0.0
148 (C99 says a function shall return "an implementation-defined value
149 whose magnitude is no greater than the smallest normalized
150 positive number in the specified type").
156 floating-point exception may be raised.
158 Some functions deliver a range error if the supplied argument value,
159 or the correct function result, would be
161 A subnormal value is one that is nonzero,
162 but with a magnitude that is so small that
163 it can't be presented in normalized form
164 (i.e., with a 1 in the most significant bit of the significand).
165 The representation of a subnormal number will contain one
166 or more leading zeros in the significand.
170 identifier specified by C99 and POSIX.1 is not supported by glibc.
171 .\" See CONFORMANCE in the glibc 2.8 (and earlier) source.
172 This identifier is supposed to indicate which of the two
173 error-notification mechanisms
175 exceptions retrievable via
176 .BR fetestexcept (3))
178 The standards require that at least one be in use,
179 but permit both to be available.
180 The current (glibc 2.8) situation under glibc is messy.
181 Most (but not all) functions raise exceptions on errors.
186 but don't raise an exception.
187 A very few functions do neither.
188 See the individual manual pages for details.
190 To avoid the complexities of using
195 it is often advised that one should instead check for bad argument
196 values before each call.
197 .\" http://www.securecoding.cert.org/confluence/display/seccode/FLP32-C.+Prevent+or+detect+domain+and+range+errors+in+math+functions
198 For example, the following code ensures that
200 argument is not a NaN and is not zero (a pole error) or
201 less than zero (a domain error):
207 if (isnan(x) || islessequal(x, 0)) {
208 /* Deal with NaN / pole error / domain error */
215 The discussion on this page does not apply to the complex
216 mathematical functions (i.e., those declared by
218 which in general are not required to return errors by C99
223 .I "\-fno\-math\-errno"
224 option causes the executable to employ implementations of some
225 mathematical functions that are faster than the standard
226 implementations, but do not set
233 .IR "\-fno\-math\-errno" .)
234 An error can still be tested for using
235 .BR fetestexcept (3).