OSDN Git Service

414eeae4855dde93a9060ba8a8f833651dbbb9d4
[linuxjm/LDP_man-pages.git] / draft / 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 .\" Japanese Version Copyright (c) 2008  Akihiro MOTOKI
25 .\"         all rights reserved.
26 .\" Translated 2008-08-17, Akihiro MOTOKI <amotoki@dd.iij4u.or.jp>, LDP v3.07
27 .\" 
28 .\"WORD:        significand     仮数部
29 .\"WORD:        domain error    領域エラー
30 .\"WORD:        pole error      極エラー
31 .\"WORD:        range error     範囲エラー
32 .\" 
33 .TH MATH_ERROR 7 2008-08-11 "Linux" "Linux Programmer's Manual"
34 .\"O .SH NAME
35 .SH 名前
36 .\"O math_error \- detecting errors from mathematical functions
37 math_error \- 数学関数からのエラーの検出
38 .\"O .SH SYNOPSIS
39 .SH 書式
40 .nf
41 .B #include <math.h>
42 .B #include <errno.h>
43 .B #include <fenv.h>
44 .fi
45 .\"O .SH DESCRIPTION
46 .SH 説明
47 .\"O When an error occurs,
48 .\"O most library functions indicate this fact by returning a special value
49 .\"O (e.g., \-1 or NULL).
50 .\"O Because they typically return a floating-point number,
51 .\"O the mathematical functions declared in
52 .\"O .IR <math.h>
53 .\"O indicate an error using other mechanisms.
54 .\"O There are two error-reporting mechanisms:
55 .\"O the older one sets
56 .\"O .IR errno ;
57 .\"O the newer one uses the floating-point exception mechanism (the use of
58 .\"O .BR feclearexcept (3)
59 .\"O and
60 .\"O .BR fetestexcept (3),
61 .\"O as outlined below)
62 .\"O described in
63 .\"O .BR fenv (3).
64 エラーが発生すると、ほとんどのライブラリ関数は (\-1 や NULL などの)
65 特別な値を返すことでエラーを通知する。
66 .I <math.h>
67 で宣言されている数学関数は、通常は浮動小数点値を返すので、
68 他の機構を使ってエラーを通知する。
69 エラー通知機構は 2 種類あり、
70 古いものが
71 .I errno
72 を設定するやり方であり、新しいものが
73 .BR fenv (3)
74 で説明されている浮動小数点例外機構である。
75 .RB ( feclearexcept (3)
76
77 .BR fetestexcept (3)
78 を使用する。これらについては以下で概要を説明している。)
79
80 .\"O A portable program that needs to check for an error from a mathematical
81 .\"O function should set
82 .\"O .I errno
83 .\"O to zero, and make the following call
84 移植性が必要なプログラムで、数学関数からのエラーを確認する必要がある場合には、
85 数学関数を呼び出す前に
86 .I errno
87 を 0 に設定し、以下を呼び出すべきである。
88 .in +4n
89 .nf
90
91 feclearexcept(FE_ALL_EXCEPT);
92 .\"O 
93 .fi
94 .in
95 .\"O before calling a mathematical function.
96 .\"Omotoki: 対応する訳は feclearexcept の引用の前にある。
97
98 .\"O Upon return from the mathematical function, if
99 .\"O .I errno
100 .\"O is nonzero, or the following call (see
101 .\"O .BR fenv (3))
102 .\"O returns nonzero
103 数学関数から返ってきた際に、
104 .I errno
105 が 0 以外か、以下の呼び出しが 0 以外を返した場合
106 .RB ( fenv (3)
107 参照)、数学関数でエラーが発生している。
108 .in +4n
109 .nf
110
111 fetestexcept(FE_INVALID | FE_DIVBYZERO | FE_OVERFLOW |
112              FE_UNDERFLOW);
113 .\"O 
114 .fi
115 .in
116 .\" enum
117 .\" {
118 .\" FE_INVALID = 0x01,
119 .\" __FE_DENORM = 0x02,
120 .\" FE_DIVBYZERO = 0x04,
121 .\" FE_OVERFLOW = 0x08,
122 .\" FE_UNDERFLOW = 0x10,
123 .\" FE_INEXACT = 0x20
124 .\" };
125 .\"O then an error occurred in the mathematical function.
126 .\"Omotoki: 対応する訳は fetestexcept の引用の前にある。
127
128 .\"O The error conditions that can occur for mathematical functions
129 .\"O are described below.
130 数学関数で発生するエラー条件については以下で説明する。
131 .\"O .SS Domain Error
132 .SS 領域エラー (domain error)
133 .\"O A
134 .\"O .I domain error
135 .\"O occurs when a mathematical function is supplied with an argument whose
136 .\"O value falls outside the domain for which the function
137 .\"O is defined (e.g., giving a negative argument to
138 .\"O .BR log (3)).
139 .\"O When a domain error occurs,
140 .\"O math functions commonly return a NaN
141 .\"O (though some functions return a different value in this case);
142 .\"O .I errno
143 .\"O is set to
144 .\"O .BR EDOM ,
145 .\"O and an "invalid"
146 .\"O .RB ( FE_INVALID )
147 .\"O floating-point exception is raised.
148 .I 領域エラー
149 が発生するのは、数学関数に渡された引き数の値がその関数が定義されている
150 領域に入っていない場合である (例えば
151 .BR log (3)
152 に負の引き数を渡した場合)。
153 領域エラーが発生すると、
154 数学関数は普通は NaN を返し
155 (同じ状況で違う値を返す関数もある)、
156 .I errno
157
158 .B EDOM
159 を設定し、「無効 (invalid)」
160 浮動小数点例外
161 .RB ( FE_INVALID )
162 を上げる。
163 .\"O .SS Pole Error
164 .SS 極エラー (pole error)
165 .\"O A
166 .\"O .I pole error
167 .\"O occurs when the mathematical result of a function is an exact infinity
168 .\"O (e.g., the logarithm of 0 is negative infinity).
169 .\"O When a pole error occurs,
170 .\"O the function returns the (signed) value
171 .\"O .BR HUGE_VAL ,
172 .\"O .BR HUGE_VALF ,
173 .\"O or
174 .\"O .BR HUGE_VALL ,
175 .\"O depending on whether the function result type is
176 .\"O .IR double ,
177 .\"O .IR float ,
178 .\"O or
179 .\"O .IR "long double" .
180 .\"O The sign of the result is that which is mathematically correct for
181 .\"O the function.
182 .I 極エラー
183 が発生するのは、関数の数学的な結果が無限大そのものとなる場合である
184 (例えば
185 0 の対数は負の無限大である)。
186 極エラーが発生すると、その関数の返り値は (符号付きの)
187 .BR HUGE_VAL ,
188 .BR HUGE_VALF ,
189 .B HUGE_VALL
190 のいずれかとなる (前記の値のうちどれが返るかは関数の返り値の型により決まり、
191 それぞれ
192 .IR double ,
193 .IR float ,
194 .I "long double"
195 に対応する)。
196 結果の符号は、その関数の数学的な定義から決定される。
197 .\"O .I errno
198 .\"O is set to
199 .\"O .BR ERANGE ,
200 .\"O and a "divide-by-zero"
201 .\"O .RB ( FE_DIVBYZERO )
202 .\"O floating-point exception is raised.
203 .I errno
204
205 .B ERANGE
206 に設定され、「0 による除算 (divide-by-zero)」
207 浮動小数点例外
208 .RB ( FE_DIVBYZERO )
209 が上がる。
210 .\"O .SS Range Error
211 .SS 範囲エラー (range エラー)
212 .\"O A
213 .\"O .I range error
214 .\"O occurs when the magnitude of the function result means that it
215 .\"O cannot be represented in the result type of the function.
216 .\"O The return value of the function depends on whether the range error
217 .\"O was an overflow or an underflow.
218 .I 範囲エラー
219 が発生するのは、関数の結果の値がその関数の返り値の型では表現できない場合
220 である。関数の返り値は、範囲エラーがオーバーフローであったかアンダーフロー
221 であったかによって異なる。
222
223 .\"O A floating result
224 .\"O .I overflows
225 .\"O if the result is finite,
226 .\"O but is too large to represented in the result type.
227 .\"O When an overflow occurs,
228 .\"O the function returns the value
229 .\"O .BR HUGE_VAL ,
230 .\"O .BR HUGE_VALF ,
231 .\"O or
232 .\"O .BR HUGE_VALL ,
233 .\"O depending on whether the function result type is
234 .\"O .IR double ,
235 .\"O .IR float ,
236 .\"O or
237 .\"O .IR "long double" .
238 .\"O .I errno
239 .\"O is set to
240 .\"O .BR ERANGE ,
241 .\"O and an "overflow"
242 .\"O .RB ( FE_OVERFLOW )
243 .\"O floating-point exception is raised.
244 浮動小数点のオーバーフローは、結果が有限だが、大き過ぎて
245 結果を返す型では表現できない場合に発生する。
246 オーバーフローが発生すると、
247 その関数は
248 .BR HUGE_VAL ,
249 .BR HUGE_VALF ,
250 .B HUGE_VALL
251 のいずれかを返す (前記の値のうちどれが返るかは関数の返り値の型により決まり、
252 それぞれ
253 .IR double ,
254 .IR float ,
255 .I "long double"
256 に対応する)。
257 .I errno
258
259 .B ERANGE
260 に設定され、「オーバーフロー (overflow)」
261 浮動小数点例外
262 .RB ( FE_OVERFLOW )
263 が上がる。
264
265 .\"O A floating result
266 .\"O .I underflows
267 .\"O if the result is too small to be represented in the result type.
268 .\"O If an underflow occurs,
269 .\"O a mathematical function typically returns 0.0
270 .\"O (C99 says a function shall return "an implementation-defined value
271 .\"O whose magnitude is no greater than the smallest normalized
272 .\"O positive number in the specified type").
273 浮動小数点のアンダーフローは、
274 結果が小さ過ぎて、結果を返す型では表現できない場合に発生する。
275 アンダーフローが発生すると、数学関数は通常は 0.0 を返す
276 (C99 では、指定された型において最小の正規化された正の値より大きくない
277 値を持つ実装定義 (implementation-defined) の値を返す、となっている)。
278 .\"O .I errno
279 .\"O may be set to
280 .\"O .BR ERANGE ,
281 .\"O and an "overflow"
282 .\"O .RB ( FE_UNDERFLOW )
283 .\"O floating-point exception may be raised.
284 .I errno
285
286 .B ERANGE
287 に設定され、「アンダーフロー」浮動小数点例外
288 .RB ( FE_UNDERFLOW )
289 が上がる。
290
291 .\"O Some functions deliver a range error if the supplied argument value,
292 .\"O or the correct function result, would be
293 .\"O .IR subnormal .
294 .\"O A subnormal value is one that is nonzero,
295 .\"O but with a magnitude that is so small that
296 .\"O it can't be presented in normalized form
297 .\"O (i.e., with a 1 in the most significant bit of the significand).
298 .\"O The representation of a subnormal number will contain one
299 .\"O or more leading zeros in the significand.
300 いくつかの関数では、渡された引き数の値や、正しい関数の結果が
301 .I subnormal (非正規化数)
302 になる場合に範囲エラーを上げる。
303 subnormal な値とは、0 ではないが、その値が小さすぎて
304 (仮数部の最上位ビットが 1 となる) 標準形では表現できないような値である。
305 subnormal な値の表現では、仮数部の上位側のビットに 1 個以上の 0 が
306 含まれることになる。
307 .\"O .SH NOTES
308 .SH 注意
309 .\"O The
310 .\"O .I math_errhandling
311 .\"O identifier specified by C99 and POSIX.1-2001 is not supported by glibc.
312 C99 と POSIX.1-2001 で規定されている
313 .I math_errhandling
314 識別子は glibc ではサポートされていない。
315 .\" See CONFORMANCE in the glibc 2.8 (and earlier) source.
316 .\"O This identifier is supposed to indicate which of the two
317 .\"O error-notification mechanisms
318 .\"O .RI ( errno ,
319 .\"O exceptions retrievable via
320 .\"O .BR fettestexcept (3))
321 .\"O is in use.
322 この識別子は、2 つのエラー通知機構
323 .RI ( errno
324
325 .BR fetestexcept (3)
326 経由で取得できる例外) のうちどちらが使用されているかを通知
327 することになっている。
328 .\"O The standards require that at least one be in use,
329 .\"O but permit both to be available.
330 .\"O The current (version 2.8) situation under glibc is messy.
331 .\"O Most (but not all) functions raise exceptions on errors.
332 .\"O Some also set
333 .\"O .IR errno .
334 .\"O A few functions set
335 .\"O .IR errno ,
336 .\"O but don't raise an exception.
337 .\"O A very few functions do neither.
338 .\"O See the individual manual pages for details.
339 標準では、少なくとも一つは使用されることが要求されているが、
340 両方とも利用可能であってもよいとされている。
341 glibc での現在の (バージョン 2.8 での) 状況はかなり混乱している。
342 ほとんどの関数 (ただし全部ではない) はエラー時に例外を上げる。
343 いくつかの関数は
344 .I errno
345 も設定する。
346 .I errno
347 を設定するが、例外を上げない関数も少しだけ存在する。
348 どちらも行わない関数もごく少数だが存在する。
349 詳細については個々のマニュアルページを参照のこと。
350
351 .\"O To avoid the complexities of using
352 .\"O .I errno
353 .\"O and
354 .\"O .BR fetestexcept (3)
355 .\"O for error checking,
356 .\"O it is often advised that one should instead check for bad argument
357 .\"O values before each call.
358 .I errno
359
360 .BR fetestexcept (3)
361 の両方を使ってエラーチェックを行うことで複雑になるのを避けるため、
362 多くの場合、関数呼び出しを行う前に不正な引き数かのチェックを行う
363 方法が推奨されている。
364 .\" http://www.securecoding.cert.org/confluence/display/seccode/FLP32-C.+Prevent+or+detect+domain+and+range+errors+in+math+functions
365 .\"O For example, the following code ensures that
366 .\"O .BR log (3)'s
367 .\"O argument is not a NaN and is not zero (a pole error) or
368 .\"O less than zero (a domain error):
369 例えば、以下のコードは、
370 .BR log (3)
371 の引き数が NaN でも (極エラーとなる) 0 でも (領域エラーとなる) 0 未満
372 でもないことを保証するものである。
373 .in +4n
374 .nf
375
376 double x, r;
377
378 if (isnan(x) || islessequal(x, 0)) {
379     /* Deal with NaN / pole error / domain error */
380 }
381
382 r = log(x);
383
384 .fi
385 .in
386 .\"O The discussion on this page does not apply to the complex
387 .\"O mathematical functions (i.e., those declared by
388 .\"O .IR <complex.h> ),
389 .\"O which in general are not required to return errors by C99
390 .\"O and POSIX.1-2001.
391 このページに書かれていることは、
392 .RI ( <complex.h>
393 で宣言されている) 複素数関数にはあてはまらない。
394 一般に、C99 や POSIX.1-2001 ではこれらの関数がエラーを返すことを
395 要求してない。
396
397 .\"O The
398 .\"O .BR gcc (1)
399 .\"O .I "-fno-math-errno"
400 .\"O option causes the executable to employ implementations of some
401 .\"O mathematical functions that are faster than the standard
402 .\"O implementations, but do not set
403 .\"O .I errno
404 .\"O on error.
405 .\"O (The
406 .\"O .BR gcc (1)
407 .\"O .I "-ffast-math"
408 .\"O option also enables
409 .\"O .IR "-fno-math-errno" .)
410 .\"O An error can still be tested for using
411 .\"O .BR fetestexcept (3).
412 .BR gcc (1)
413
414 .I "-fno-math-errno"
415 オプションを使うと、実行ファイルで、標準の実装よりも高速な数学関数の
416 実装が使用されるようになるが、
417 エラー時に
418 .I errno
419 が設定されない
420 .RB ( gcc (1)
421
422 .I "-ffast-math"
423 オプションを指定した場合にも
424 .I "-fno-math-errno"
425 は有効になる)。
426 このオプションを指定した場合でも、
427 .BR fetestexcept (3)
428 を使ったエラーの検査は可能である。
429 .\"O .SH SEE ALSO
430 .SH 関連項目
431 .BR gcc (1),
432 .BR errno (3),
433 .BR fenv (3),
434 .BR fpclassify (3),
435 .BR INFINITY (3),
436 .BR isgreater (3),
437 .BR matherr (3),
438 .BR nan (3)
439 .br
440 .I "info libc"