OSDN Git Service

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