OSDN Git Service

(split) LDP: draft snapshots from latest ja.po(s).
[linuxjm/LDP_man-pages.git] / draft / man3 / matherr.3
1 .\" t
2 .\" Copyright (c) 2008, Linux Foundation, written by Michael Kerrisk
3 .\"     <mtk.manpages@gmail.com>
4 .\"
5 .\" Permission is granted to make and distribute verbatim copies of this
6 .\" manual provided the copyright notice and this permission notice are
7 .\" preserved on all copies.
8 .\"
9 .\" Permission is granted to copy and distribute modified versions of this
10 .\" manual under the conditions for verbatim copying, provided that the
11 .\" entire resulting derived work is distributed under the terms of a
12 .\" permission notice identical to this one.
13 .\"
14 .\" Since the Linux kernel and libraries are constantly changing, this
15 .\" manual page may be incorrect or out-of-date.  The author(s) assume no
16 .\" responsibility for errors or omissions, or for damages resulting from
17 .\" the use of the information contained herein.  The author(s) may not
18 .\" have taken the same level of care in the production of this manual,
19 .\" which is licensed free of charge, as they might when working
20 .\" professionally.
21 .\"
22 .\" Formatted or processed versions of this manual, if unaccompanied by
23 .\" the source, must acknowledge the copyright and authors of this work.
24 .\"
25 .\"*******************************************************************
26 .\"
27 .\" This file was generated with po4a. Translate the source file.
28 .\"
29 .\"*******************************************************************
30 .TH MATHERR 3 2010\-09\-10 Linux "Linux Programmer's Manual"
31 .SH 名前
32 matherr \- SVID 数学ライブラリの例外処理
33 .SH 書式
34 .nf
35 \fB#define _SVID_SOURCE\fP             /* feature_test_macros(7) 参照 */
36 \fB#include <math.h>\fP
37
38 \fBint matherr(struct exception *\fP\fIexc\fP\fB);\fP
39
40 \fBextern _LIB_VERSION_TYPE _LIB_VERSION;\fP
41 .fi
42 .sp
43 \fI\-lm\fP でリンクする。
44 .SH 説明
45 System V Interface Definition (SVID) では、各種の数学関数は数学的な
46 例外を検出した場合に \fBmatherr\fP() を呼ばれる関数を起動すべきである、
47 と規定されている。この関数は数学関数が返る前に呼び出される。
48 \fBmatherr\fP() が返った後に、システムは数学関数に戻り、
49 それから呼び出し元に返る。
50
51 \fBmatherr\fP() の仕組みは glibc によりサポートされているが、
52 現在は廃止予定の扱いである。
53 新しくアプリケーションを作成する際には、
54 \fBmath_error\fP(7) と \fBfenv\fP(3) で説明されている手法を使用すべきである。
55 このマニュアルページでは、古いアプリケーションを保守したり移植する際の
56 助けとなるよう、 glibc の \fBmatherr\fP() の仕組みについて説明する。
57
58 \fBmatherr\fP() を使用するためには、
59 プログラマは (\fIどの\fPヘッダファイルをインクルードするよりも前に)
60 \fB_SVID_SOURCE\fP 機能検査マクロを定義し、値 \fB_SVID_\fP をグローバル変数
61 \fB_LIB_VERSION\fP に代入しなければならない。
62
63 デフォルト版の \fBmatherr\fP() がシステムによって提供されている。
64 デフォルト版は何も行わず、0 を返す (このことの重要性については
65 下記を参照)。プログラマが \fBmatherr\fP() を定義することで、
66 デフォルト版を上書きすることができる。
67 プログラマが定義した関数は例外が発生した際に起動される。
68 この関数は引き数 1 個で起動され、その引き数は以下に示す
69 \fIexception\fP 構造体へのポインタである。
70
71 .in +4n
72 .nf
73 struct exception {
74     int    type;      /* Exception type */
75     char  *name;      /* Name of function causing exception */
76     double arg1;      /* 1st argument to function */
77     double arg2;      /* 2nd argument to function */
78     double retval;    /* Function return value */
79 }
80 .fi
81 .in
82 .PP
83 \fItype\fP フィールドは以下の値のいずれかである。
84 .TP  12
85 \fBDOMAIN\fP
86 領域エラー (domain error) が発生した
87 (関数の引き数が関数が定義された範囲外であった)。
88 返り値は関数によって異なり、 \fIerror\fP には \fBEDOM\fP が設定される。
89 .TP 
90 \fBSING\fP
91 極エラー (pole error) が発生した (関数の結果が無限大である)。
92 返り値はほとんどの場合 \fBHUGE\fP (最大の単精度浮動小数点数) となり、
93 たいていは符号付きである。
94 ほとんどの場合、\fIerrno\fP には \fBEDOM\fP が設定される。
95 .TP 
96 \fBOVERFLOW\fP
97 オーバーフローが発生した。
98 ほとんどの場合、値 \fBHUGE\fP が返され、
99 \fIerrno\fP には \fBERANGE\fP が設定される。
100 .TP 
101 \fBUNDERFLOW\fP
102 アンダーフローが発生した。
103 0.0 が返され、 \fIerrno\fP に \fBERANGE\fP が設定される。
104 .TP 
105 \fBTLOSS\fP
106 Total loss of significance が発生した。
107 0.0 が返され、 \fIerrno\fP に \fBERANGE\fP が設定される。
108 .TP 
109 \fBPLOSS\fP
110 Partial loss of significance が発生した。
111 この値は glibc (や他の多くのシステム) で使用されていない。
112 .PP
113 フィールド \fIarg1\fP と \fIarg2\fP は関数に渡された引き数である
114 (引き数を一つしか取らない関数の場合は \fIarg2\fP は不定となる)。
115
116 \fIretval\fP フィールドはその数学関数が呼び出し元に返そうとしている返り値
117 を示す。プログラマが定義した \fBmatherr\fP() でこのフィールドを変更する
118 ことで、その数学関数の返り値を変更することができる。
119
120 \fBmatherr\fP() 関数が 0 を返した場合、
121 システムは \fIerrno\fP を上記の通り設定し、標準エラー出力に
122 エラーメッセージを表示することがある (下記参照)。
123
124 \fBmatherr\fP() 関数が 0 以外の値を返した場合、
125 システムは \fIerrno\fP を設定せず、エラーメッセージの表示も行わない。
126 .SS "matherr() を利用している数学関数"
127 下記の表は、関数と \fBmatherr\fP() が呼び出される状況の一覧である。
128 "Type" 列 は \fBmatherr\fP() が呼び出される際に \fIexc\->type\fP に
129 設定される値を示す。 "Result" 列は \fIexc\->retval\fP に
130 設定されるデフォルトの返り値を示す。
131
132 "Msg?" 列と "errno" 列は \fBmatherr\fP() が 0 を返した場合のデフォルトの
133 動作を示す。 "Msg?" 列に "y" が入っている場合、システムは標準エラー
134 出力にエラーメッセージを表示する。
135
136 以下の表では、下記の記法と省略形を使用している。
137 .RS
138 .nf
139
140 x        関数の最初の引き数
141 y        関数の二番目の引き数
142 fin      引き数の値が無限大
143 neg      引き数が負の値
144 int      引き数が整数値
145 o/f      結果のオーバーフロー
146 u/f      結果のアンダーフロー
147 |x|      x の絶対値
148 X_TLOSS  \fI<math.h>\fP で定義される定数
149 .fi
150 .RE
151 .\" Details below from glibc 2.8's sysdeps/ieee754/k_standard.c
152 .\" A subset of cases were test by experimental programs.
153 .TS
154 lB lB lB cB lB
155 l l l c l.
156 Function        Type    Result  Msg?    errno
157 acos(|x|>1)     DOMAIN  HUGE    y       EDOM
158 asin(|x|>1)     DOMAIN  HUGE    y       EDOM
159 atan2(0,0)      DOMAIN  HUGE    y       EDOM
160 .\" retval is 0.0/0.0
161 acosh(x<1)      DOMAIN  NAN     y       EDOM    
162 .\" retval is 0.0/0.0
163 atanh(|x|>1)    DOMAIN  NAN     y       EDOM    
164 .\" retval is x/0.0
165 atanh(|x|==1)   SING    (x>0.0)?        y       EDOM    
166 \       \       HUGE_VAL :
167 \       \       \-HUGE_VAL
168 cosh(fin) o/f   OVERFLOW        HUGE    n       ERANGE
169 sinh(fin) o/f   OVERFLOW        (x>0.0) ?       n       ERANGE
170 \       \       HUGE : \-HUGE
171 sqrt(x<0)       DOMAIN  0.0     y       EDOM
172 hypot(fin,fin) o/f      OVERFLOW        HUGE    n       ERANGE
173 exp(fin) o/f    OVERFLOW        HUGE    n       ERANGE
174 exp(fin) u/f    UNDERFLOW       0.0     n       ERANGE
175 exp2(fin) o/f   OVERFLOW        HUGE    n       ERANGE
176 exp2(fin) u/f   UNDERFLOW       0.0     n       ERANGE
177 exp10(fin) o/f  OVERFLOW        HUGE    n       ERANGE
178 exp10(fin) u/f  UNDERFLOW       0.0     n       ERANGE
179 j0(|x|>X_TLOSS) TLOSS   0.0     y       ERANGE
180 j1(|x|>X_TLOSS) TLOSS   0.0     y       ERANGE
181 jn(|x|>X_TLOSS) TLOSS   0.0     y       ERANGE
182 y0(x>X_TLOSS)   TLOSS   0.0     y       ERANGE
183 y1(x>X_TLOSS)   TLOSS   0.0     y       ERANGE
184 yn(x>X_TLOSS)   TLOSS   0.0     y       ERANGE
185 y0(0)   DOMAIN  \-HUGE  y       EDOM
186 y0(x<0) DOMAIN  \-HUGE  y       EDOM
187 y1(0)   DOMAIN  \-HUGE  y       EDOM
188 y1(x<0) DOMAIN  \-HUGE  y       EDOM
189 yn(n,0) DOMAIN  \-HUGE  y       EDOM
190 yn(x<0) DOMAIN  \-HUGE  y       EDOM
191 lgamma(fin) o/f OVERFLOW        HUGE    n       ERANGE
192 lgamma(\-int) or        SING    HUGE    y       EDOM
193 \ \ lgamma(0)
194 tgamma(fin) o/f OVERFLOW        HUGE_VAL        n       ERANGE
195 tgamma(\-int)   SING    NAN     y       EDOM
196 tgamma(0)       SING    copysign(       y       ERANGE
197 \       \       HUGE_VAL,x)
198 log(0)  SING    \-HUGE  y       EDOM
199 log(x<0)        DOMAIN  \-HUGE  y       EDOM
200 .\" different from log()
201 log2(0) SING    \-HUGE  n       EDOM    
202 .\" different from log()
203 log2(x<0)       DOMAIN  \-HUGE  n       EDOM    
204 log10(0)        SING    \-HUGE  y       EDOM
205 log10(x<0)      DOMAIN  \-HUGE  y       EDOM
206 pow(0.0,0.0)    DOMAIN  0.0     y       EDOM
207 pow(x,y) o/f    OVERFLOW        HUGE    n       ERANGE
208 pow(x,y) u/f    UNDERFLOW       0.0     n       ERANGE
209 pow(NaN,0.0)    DOMAIN  x       n       EDOM
210 .\" +0 and -0
211 0**neg  DOMAIN  0.0     y       EDOM    
212 neg**non\-int   DOMAIN  0.0     y       EDOM
213 scalb() o/f     OVERFLOW        (x>0.0) ?       n       ERANGE
214 \       \       HUGE_VAL :
215 \       \       \-HUGE_VAL
216 scalb() u/f     UNDERFLOW       copysign(       n       ERANGE
217 \       \       \ \ 0.0,x)
218 fmod(x,0)       DOMAIN  x       y       EDOM
219 .\" retval is 0.0/0.0
220 remainder(x,0)  DOMAIN  NAN     y       EDOM    
221 .TE
222 .SH 例
223 以下のサンプルプログラムは \fBlog\fP(3) を呼び出した際の
224 \fBmatherr\fP() の使用法を示したものである。
225 最初の引き数は \fBlog\fP(3) に渡す浮動小数点数である。
226 省略可能な第二引き数を指定した場合、
227 \fB_LIB_VERSION\fP に \fB_SVID_\fP が設定され、
228 \fBmatherr\fP() が呼ばれるようになる。
229 このコマンドライン引き数で指定した整数は、
230 \fBmatherr\fP() からの返り値として使用される。
231 省略可能な第三引き数を指定した場合、 \fBmatherr\fP() は
232 数学関数の返り値として代わりに引き数で指定した値を割り当てる。
233
234 以下の実行例では、
235 \fBlog\fP(3) に引き数 0.0 が渡しているが、
236 \fBmatherr\fP() は使用しない。
237
238 .in +4n
239 .nf
240 $\fB ./a.out 0.0\fP
241 errno: Numerical result out of range
242 x=\-inf
243 .fi
244 .in
245
246 以下の実行例では、
247 \fBmatherr\fP() が呼び出され、返り値 0 が返される。
248
249 .in +4n
250 .nf
251 $\fB ./a.out 0.0 0\fP
252 matherr SING exception in log() function
253         args:   0.000000, 0.000000
254         retval: \-340282346638528859811704183484516925440.000000
255 log: SING error
256 errno: Numerical argument out of domain
257 x=\-340282346638528859811704183484516925440.000000
258 .fi
259 .in
260
261 メッセージ "log: SING error" は C ライブラリによって出力されている。
262
263 次の実行例では、 \fBmatherr\fP() が呼び出され、0 以外の返り値が返される。
264
265 .in +4n
266 .nf
267 $\fB ./a.out 0.0 1\fP
268 matherr SING exception in log() function
269         args:   0.000000, 0.000000
270         retval: \-340282346638528859811704183484516925440.000000
271 x=\-340282346638528859811704183484516925440.000000
272 .fi
273 .in
274
275 この場合は、C ライブラリはメッセージを出力しておらず、
276 \fIerrno\fP は設定されていない。
277
278 次の実行例では、 \fBmatherr\fP() が呼び出され、
279 数学関数の返り値が変更され、0 以外の返り値が返されている。
280
281 .in +4n
282 .nf
283 $\fB ./a.out 0.0 1 12345.0\fP
284 matherr SING exception in log() function
285         args:   0.000000, 0.000000
286         retval: \-340282346638528859811704183484516925440.000000
287 x=12345.000000
288 .fi
289 .in
290 .SS プログラムのソース
291 \&
292 .nf
293 #define _SVID_SOURCE
294 #include <errno.h>
295 #include <math.h>
296 #include <stdio.h>
297 #include <stdlib.h>
298
299 static int matherr_ret = 0;     /* Value that matherr()
300                                    should return */
301 static int change_retval = 0;   /* Should matherr() change
302                                    function\(aqs return value? */
303 static double new_retval;       /* New function return value */
304
305 int
306 matherr(struct exception *exc)
307 {
308     fprintf(stderr, "matherr %s exception in %s() function\en",
309            (exc\->type == DOMAIN) ?    "DOMAIN" :
310            (exc\->type == OVERFLOW) ?  "OVERFLOW" :
311            (exc\->type == UNDERFLOW) ? "UNDERFLOW" :
312            (exc\->type == SING) ?      "SING" :
313            (exc\->type == TLOSS) ?     "TLOSS" :
314            (exc\->type == PLOSS) ?     "PLOSS" : "???",
315             exc\->name);
316     fprintf(stderr, "        args:   %f, %f\en",
317             exc\->arg1, exc\->arg2);
318     fprintf(stderr, "        retval: %f\en", exc\->retval);
319
320     if (change_retval)
321         exc\->retval = new_retval;
322
323     return matherr_ret;
324 }
325
326 int
327 main(int argc, char *argv[])
328 {
329     double x;
330
331     if (argc < 2) {
332         fprintf(stderr, "Usage: %s <argval>"
333                 " [<matherr\-ret> [<new\-func\-retval>]]\en", argv[0]);
334         exit(EXIT_FAILURE);
335     }
336
337     if (argc > 2) {
338         _LIB_VERSION = _SVID_;
339         matherr_ret = atoi(argv[2]);
340     }
341
342     if (argc > 3) {
343         change_retval = 1;
344         new_retval = atof(argv[3]);
345     }
346
347     x = log(atof(argv[1]));
348     if (errno != 0)
349         perror("errno");
350
351     printf("x=%f\en", x);
352     exit(EXIT_SUCCESS);
353 }
354 .fi
355 .SH 関連項目
356 \fBfenv\fP(3), \fBmath_error\fP(7), \fBstandards\fP(7)
357 .SH この文書について
358 この man ページは Linux \fIman\-pages\fP プロジェクトのリリース 3.41 の一部
359 である。プロジェクトの説明とバグ報告に関する情報は
360 http://www.kernel.org/doc/man\-pages/ に書かれている。