OSDN Git Service

(split) Import translated manuals from JM CVS Repository.
[linuxjm/LDP_man-pages.git] / original / 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-08-11 "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 When an error occurs,
35 most library functions indicate this fact by returning a special value
36 (e.g., \-1 or NULL).
37 Because they typically return a floating-point number,
38 the mathematical functions declared in
39 .IR <math.h>
40 indicate an error using other mechanisms.
41 There are two error-reporting mechanisms:
42 the older one sets
43 .IR errno ;
44 the newer one uses the floating-point exception mechanism (the use of
45 .BR feclearexcept (3)
46 and
47 .BR fetestexcept (3),
48 as outlined below)
49 described in
50 .BR fenv (3).
51
52 A portable program that needs to check for an error from a mathematical
53 function should set
54 .I errno
55 to zero, and make the following call
56 .in +4n
57 .nf
58
59 feclearexcept(FE_ALL_EXCEPT);
60
61 .fi
62 .in
63 before calling a mathematical function.
64
65 Upon return from the mathematical function, if
66 .I errno
67 is nonzero, or the following call (see
68 .BR fenv (3))
69 returns nonzero
70 .in +4n
71 .nf
72
73 fetestexcept(FE_INVALID | FE_DIVBYZERO | FE_OVERFLOW |
74              FE_UNDERFLOW);
75
76 .fi
77 .in
78 .\" enum
79 .\" {
80 .\" FE_INVALID = 0x01,
81 .\" __FE_DENORM = 0x02,
82 .\" FE_DIVBYZERO = 0x04,
83 .\" FE_OVERFLOW = 0x08,
84 .\" FE_UNDERFLOW = 0x10,
85 .\" FE_INEXACT = 0x20
86 .\" };
87 then an error occurred in the mathematical function.
88
89 The error conditions that can occur for mathematical functions
90 are described below.
91 .SS Domain Error
92 A
93 .I domain error
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
97 .BR log (3)).
98 When a domain error occurs,
99 math functions commonly return a NaN
100 (though some functions return a different value in this case);
101 .I errno
102 is set to
103 .BR EDOM ,
104 and an "invalid"
105 .RB ( FE_INVALID )
106 floating-point exception is raised.
107 .SS Pole Error
108 A
109 .I pole error
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
114 .BR HUGE_VAL ,
115 .BR HUGE_VALF ,
116 or
117 .BR HUGE_VALL ,
118 depending on whether the function result type is
119 .IR double ,
120 .IR float ,
121 or
122 .IR "long double" .
123 The sign of the result is that which is mathematically correct for
124 the function.
125 .I errno
126 is set to
127 .BR ERANGE ,
128 and a "divide-by-zero"
129 .RB ( FE_DIVBYZERO )
130 floating-point exception is raised.
131 .SS Range Error
132 A
133 .I range error
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.
138
139 A floating result
140 .I overflows
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
145 .BR HUGE_VAL ,
146 .BR HUGE_VALF ,
147 or
148 .BR HUGE_VALL ,
149 depending on whether the function result type is
150 .IR double ,
151 .IR float ,
152 or
153 .IR "long double" .
154 .I errno
155 is set to
156 .BR ERANGE ,
157 and an "overflow"
158 .RB ( FE_OVERFLOW )
159 floating-point exception is raised.
160
161 A floating result
162 .I underflows
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 .I errno
170 may be set to
171 .BR ERANGE ,
172 and an "overflow"
173 .RB ( FE_UNDERFLOW )
174 floating-point exception may be raised.
175
176 Some functions deliver a range error if the supplied argument value,
177 or the correct function result, would be
178 .IR subnormal .
179 A subnormal value is one that is nonzero,
180 but with a magnitude that is so small that
181 it can't be presented in normalized form
182 (i.e., with a 1 in the most significant bit of the significand).
183 The representation of a subnormal number will contain one
184 or more leading zeros in the significand.
185 .SH NOTES
186 The
187 .I math_errhandling
188 identifier specified by C99 and POSIX.1-2001 is not supported by glibc.
189 .\" See CONFORMANCE in the glibc 2.8 (and earlier) source.
190 This identifier is supposed to indicate which of the two
191 error-notification mechanisms
192 .RI ( errno ,
193 exceptions retrievable via
194 .BR fettestexcept (3))
195 is in use.
196 The standards require that at least one be in use,
197 but permit both to be available.
198 The current (version 2.8) situation under glibc is messy.
199 Most (but not all) functions raise exceptions on errors.
200 Some also set
201 .IR errno .
202 A few functions set
203 .IR errno ,
204 but don't raise an exception.
205 A very few functions do neither.
206 See the individual manual pages for details.
207
208 To avoid the complexities of using
209 .I errno
210 and
211 .BR fetestexcept (3)
212 for error checking,
213 it is often advised that one should instead check for bad argument
214 values before each call.
215 .\" http://www.securecoding.cert.org/confluence/display/seccode/FLP32-C.+Prevent+or+detect+domain+and+range+errors+in+math+functions
216 For example, the following code ensures that
217 .BR log (3)'s
218 argument is not a NaN and is not zero (a pole error) or
219 less than zero (a domain error):
220 .in +4n
221 .nf
222
223 double x, r;
224
225 if (isnan(x) || islessequal(x, 0)) {
226     /* Deal with NaN / pole error / domain error */
227 }
228
229 r = log(x);
230
231 .fi
232 .in
233 The discussion on this page does not apply to the complex
234 mathematical functions (i.e., those declared by
235 .IR <complex.h> ),
236 which in general are not required to return errors by C99
237 and POSIX.1-2001.
238
239 The
240 .BR gcc (1)
241 .I "-fno-math-errno"
242 option causes the executable to employ implementations of some
243 mathematical functions that are faster than the standard
244 implementations, but do not set
245 .I errno
246 on error.
247 (The
248 .BR gcc (1)
249 .I "-ffast-math"
250 option also enables
251 .IR "-fno-math-errno" .)
252 An error can still be tested for using
253 .BR fetestexcept (3).
254 .SH SEE ALSO
255 .BR gcc (1),
256 .BR errno (3),
257 .BR fenv (3),
258 .BR fpclassify (3),
259 .BR INFINITY (3),
260 .BR isgreater (3),
261 .BR matherr (3),
262 .BR nan (3)
263 .br
264 .I "info libc"