Debian Codegraph @

upstream/debian-unstable/man7/math_error.7

raw
1 .\" Copyright, the authors of the Linux man-pages project
2 .\"
3 .\" SPDX-License-Identifier: Linux-man-pages-copyleft
4 .\"
5 .TH math_error 7 2025-05-17 "Linux man-pages 6.18"
6 .SH NAME
7 math_error \- detecting errors from mathematical functions
8 .SH SYNOPSIS
9 .nf
10 .B #include <math.h>
11 .B #include <errno.h>
12 .B #include <fenv.h>
13 .fi
14 .SH DESCRIPTION
15 When an error occurs,
16 most library functions indicate this fact by returning a special value
17 (e.g., \-1 or NULL).
18 Because they typically return a floating-point number,
19 the mathematical functions declared in
20 .I <math.h>
21 indicate an error using other mechanisms.
22 There are two error-reporting mechanisms:
23 the older one sets
24 .IR errno ;
25 the newer one uses the floating-point exception mechanism (the use of
26 .BR feclearexcept (3)
27 and
28 .BR fetestexcept (3),
29 as outlined below)
30 described in
31 .BR fenv (3).
32 .P
33 A portable program that needs to check for an error from a mathematical
34 function should set
35 .I errno
36 to zero, and make the following call
37 .P
38 .in +4n
39 .EX
40 feclearexcept(FE_ALL_EXCEPT);
41 .EE
42 .in
43 .P
44 before calling a mathematical function.
45 .P
46 Upon return from the mathematical function, if
47 .I errno
48 is nonzero, or the following call (see
49 .BR fenv (3))
50 returns nonzero
51 .P
52 .in +4n
53 .EX
54 fetestexcept(FE_INVALID | FE_DIVBYZERO | FE_OVERFLOW |
55 FE_UNDERFLOW);
56 .EE
57 .in
58 .P
59 .\" enum
60 .\" {
61 .\" FE_INVALID = 0x01,
62 .\" __FE_DENORM = 0x02,
63 .\" FE_DIVBYZERO = 0x04,
64 .\" FE_OVERFLOW = 0x08,
65 .\" FE_UNDERFLOW = 0x10,
66 .\" FE_INEXACT = 0x20
67 .\" };
68 then an error occurred in the mathematical function.
69 .P
70 The error conditions that can occur for mathematical functions
71 are described below.
72 .SS Domain error
73 A
74 .I domain error
75 occurs when a mathematical function is supplied with an argument whose
76 value falls outside the domain for which the function
77 is defined (e.g., giving a negative argument to
78 .BR log (3)).
79 When a domain error occurs,
80 math functions commonly return a NaN
81 (though some functions return a different value in this case);
82 .I errno
83 is set to
84 .BR EDOM ,
85 and an "invalid"
86 .RB ( FE_INVALID )
87 floating-point exception is raised.
88 .SS Pole error
89 A
90 .I pole error
91 occurs when the mathematical result of a function is an exact infinity
92 (e.g., the logarithm of 0 is negative infinity).
93 When a pole error occurs,
94 the function returns the (signed) value
95 .BR HUGE_VAL ,
96 .BR HUGE_VALF ,
97 or
98 .BR HUGE_VALL ,
99 depending on whether the function result type is
100 .IR double ,
101 .IR float ,
102 or
103 .IR "long double" .
104 The sign of the result is that which is mathematically correct for
105 the function.
106 .I errno
107 is set to
108 .BR ERANGE ,
109 and a "divide-by-zero"
110 .RB ( FE_DIVBYZERO )
111 floating-point exception is raised.
112 .SS Range error
113 A
114 .I range error
115 occurs when the magnitude of the function result means that it
116 cannot be represented in the result type of the function.
117 The return value of the function depends on whether the range error
118 was an overflow or an underflow.
119 .P
120 A floating result
121 .I overflows
122 if the result is finite,
123 but is too large to represented in the result type.
124 When an overflow occurs,
125 the function returns the value
126 .BR HUGE_VAL ,
127 .BR HUGE_VALF ,
128 or
129 .BR HUGE_VALL ,
130 depending on whether the function result type is
131 .IR double ,
132 .IR float ,
133 or
134 .IR "long double" .
135 .I errno
136 is set to
137 .BR ERANGE ,
138 and an "overflow"
139 .RB ( FE_OVERFLOW )
140 floating-point exception is raised.
141 .P
142 A floating result
143 .I underflows
144 if the result is too small to be represented in the result type.
145 If an underflow occurs,
146 a mathematical function typically returns 0.0
147 (C99 says a function shall return "an implementation-defined value
148 whose magnitude is no greater than the smallest normalized
149 positive number in the specified type").
150 .I errno
151 may be set to
152 .BR ERANGE ,
153 and an "underflow"
154 .RB ( FE_UNDERFLOW )
155 floating-point exception may be raised.
156 .P
157 Some functions deliver a range error if the supplied argument value,
158 or the correct function result, would be
159 .IR subnormal .
160 A subnormal value is one that is nonzero,
161 but with a magnitude that is so small that
162 it can't be presented in normalized form
163 (i.e., with a 1 in the most significant bit of the significand).
164 The representation of a subnormal number will contain one
165 or more leading zeros in the significand.
166 .SH NOTES
167 The
168 .I math_errhandling
169 identifier specified by C99 and POSIX.1 is not supported by glibc.
170 .\" See CONFORMANCE in the glibc 2.8 (and earlier) source.
171 This identifier is supposed to indicate which of the two
172 error-notification mechanisms
173 .RI ( errno ,
174 exceptions retrievable via
175 .BR fetestexcept (3))
176 is in use.
177 The standards require that at least one be in use,
178 but permit both to be available.
179 The current (glibc 2.8) situation under glibc is messy.
180 Most (but not all) functions raise exceptions on errors.
181 Some also set
182 .IR errno .
183 A few functions set
184 .IR errno ,
185 but don't raise an exception.
186 A very few functions do neither.
187 See the individual manual pages for details.
188 .P
189 To avoid the complexities of using
190 .I errno
191 and
192 .BR fetestexcept (3)
193 for error checking,
194 it is often advised that one should instead check for bad argument
195 values before each call.
196 .\" http://www.securecoding.cert.org/confluence/display/seccode/FLP32-C.+Prevent+or+detect+domain+and+range+errors+in+math+functions
197 For example, the following code ensures that
198 .BR log (3)'s
199 argument is not a NaN and is not zero (a pole error) or
200 less than zero (a domain error):
201 .P
202 .in +4n
203 .EX
204 double x, r;
205 \&
206 if (isnan(x) || islessequal(x, 0)) {
207 /* Deal with NaN / pole error / domain error */
208 }
209 \&
210 r = log(x);
211 .EE
212 .in
213 .P
214 The discussion on this page does not apply to the complex
215 mathematical functions (i.e., those declared by
216 .IR <complex.h> ),
217 which in general are not required to return errors by C99
218 and POSIX.1.
219 .P
220 The
221 .BR gcc (1)
222 .I "\-fno\-math\-errno"
223 option causes the executable to employ implementations of some
224 mathematical functions that are faster than the standard
225 implementations, but do not set
226 .I errno
227 on error.
228 (The
229 .BR gcc (1)
230 .I "\-ffast\-math"
231 option also enables
232 .IR "\-fno\-math\-errno" .)
233 An error can still be tested for using
234 .BR fetestexcept (3).
235 .SH SEE ALSO
236 .BR gcc (1),
237 .BR errno (3),
238 .BR fenv (3),
239 .BR fpclassify (3),
240 .BR INFINITY (3),
241 .BR isgreater (3),
242 .BR matherr (3),
243 .BR nan (3)
244 .P
245 .I "info libc"