File: matherr.3

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

static int matherr_ret = 0;     /* Value that matherr()
                                   should return */
static int change_retval = 0;   /* Should matherr() change
                                   function\(aqs return value? */
static double new_retval;       /* New function return value */

int
matherr(struct exception *exc)
{
    fprintf(stderr, "matherr %s exception in %s() function\en",
           (exc\->type == DOMAIN) ?    "DOMAIN" :
           (exc\->type == OVERFLOW) ?  "OVERFLOW" :
           (exc\->type == UNDERFLOW) ? "UNDERFLOW" :
           (exc\->type == SING) ?      "SING" :
           (exc\->type == TLOSS) ?     "TLOSS" :
           (exc\->type == PLOSS) ?     "PLOSS" : "???",
            exc\->name);
    fprintf(stderr, "        args:   %f, %f\en",
            exc\->arg1, exc\->arg2);
    fprintf(stderr, "        retval: %f\en", exc\->retval);

    if (change_retval)
        exc\->retval = new_retval;

    return matherr_ret;
}

int
main(int argc, char *argv[])
{
    double x;

    if (argc < 2) {
        fprintf(stderr, "Usage: %s <argval>"
                " [<matherr\-ret> [<new\-func\-retval>]]\en", argv[0]);
        exit(EXIT_FAILURE);
    }

    if (argc > 2) {
        _LIB_VERSION = _SVID_;
        matherr_ret = atoi(argv[2]);
    }

    if (argc > 3) {
        change_retval = 1;
        new_retval = atof(argv[3]);
    }

    x = log(atof(argv[1]));
    if (errno != 0)
        perror("errno");

    printf("x=%f\en", x);
    exit(EXIT_SUCCESS);
}
.EE
.SH 関連項目
\fBfenv\fP(3), \fBmath_error\fP(7), \fBstandards\fP(7)
.SH この文書について
この man ページは Linux \fIman\-pages\fP プロジェクトのリリース 5.10 の一部である。プロジェクトの説明とバグ報告に関する情報は
\%https://www.kernel.org/doc/man\-pages/ に書かれている。