OSDN Git Service

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