]> git.ipfire.org Git - thirdparty/man-pages.git/blame - man3/matherr.3
fanotify_init.2: ffix
[thirdparty/man-pages.git] / man3 / matherr.3
CommitLineData
5f1f495c
MK
1'\" t
2.\" Copyright (c) 2008, Linux Foundation, written by Michael Kerrisk
3.\" <mtk.manpages@gmail.com>
4.\"
93015253 5.\" %%%LICENSE_START(VERBATIM)
5f1f495c
MK
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.
4b72fb64 25.\" %%%LICENSE_END
5f1f495c 26.\"
97986708 27.TH MATHERR 3 2016-03-15 "Linux" "Linux Programmer's Manual"
5f1f495c
MK
28.SH NAME
29matherr \- SVID math library exception handling
30.SH SYNOPSIS
31.nf
5f1f495c
MK
32.B #include <math.h>
33
34.BI "int matherr(struct exception *" exc );
35
36.B extern _LIB_VERSION_TYPE _LIB_VERSION;
37.fi
38.sp
636ed4d5
MK
39.in -4n
40Feature Test Macro Requirements for glibc (see
41.BR feature_test_macros (7)):
42
43.in
44.ad l
45.BR significand (),
46.BR significandf (),
47.BR significandl ():
48.RS 4
49/* Since glibc 2.19: */ _DEFAULT_SOURCE
50 || /* Glibc versions <= 2.19: */ _SVID_SOURCE
51.RE
52.ad b
53.sp
5f1f495c
MK
54Link with \fI\-lm\fP.
55.SH DESCRIPTION
56The System V Interface Definition (SVID) specifies that various
57math functions should invoke a function called
58.BR matherr ()
59if a math exception is detected.
60This function is called before the math function returns;
61after
62.BR matherr ()
63returns, the system then returns to the math function,
64which in turn returns to the caller.
65
66The
67.BR matherr ()
68mechanism is supported by glibc, but is now obsolete:
69new applications should use the techniques described in
70.BR math_error (7)
71and
72.BR fenv (3).
73This page documents the glibc
74.BR matherr ()
75mechanism as an aid for maintaining and porting older applications.
76
77To employ
78.BR matherr (),
79the programmer must define the
80.B _SVID_SOURCE
e417acb0
MK
81feature test macro
82(before including
83.I any
84header files),
85and assign the value
5f1f495c
MK
86.B _SVID_
87to the external variable
88.BR _LIB_VERSION .
89
90The system provides a default version of
91.BR matherr ().
92This version does nothing, and returns zero
93(see below for the significance of this).
94The default
95.BR matherr ()
96can be overridden by a programmer-defined
97version, which will be invoked when an exception occurs.
98The function is invoked with one argument, a pointer to an
99.I exception
100structure, defined as follows:
101
102.in +4n
103.nf
104struct exception {
105 int type; /* Exception type */
106 char *name; /* Name of function causing exception */
107 double arg1; /* 1st argument to function */
108 double arg2; /* 2nd argument to function */
109 double retval; /* Function return value */
110}
111.fi
112.in
113.PP
114The
115.I type
116field has one of the following values:
117.TP 12
118.B DOMAIN
119A domain error occurred (the function argument was outside the range
120for which the function is defined).
121The return value depends on the function;
122.I errno
123is set to
124.BR EDOM .
125.TP
126.B SING
127A pole error occurred (the function result is an infinity).
128The return value in most cases is
129.B HUGE
130(the largest single precision floating-point number),
131appropriately signed.
132In most cases,
133.I errno
134is set to
135.BR EDOM .
136.TP
137.B OVERFLOW
138An overflow occurred.
139In most cases, the value
140.B HUGE
141is returned, and
142.I errno
143is set to
144.BR ERANGE .
145.TP
146.B UNDERFLOW
147An underflow occurred.
1480.0 is returned, and
149.I errno
150is set to
151.BR ERANGE .
152.TP
153.B TLOSS
154Total loss of significance.
1550.0 is returned, and
156.I errno
157is set to
158.BR ERANGE .
159.TP
160.B PLOSS
161Partial loss of significance.
162This value is unused on glibc
163(and many other systems).
164.PP
165The
166.I arg1
167and
168.I arg2
169fields are the arguments supplied to the function
170.RI ( arg2
171is undefined for functions that take only one argument).
172
173The
174.I retval
175field specifies the return value that the math
176function will return to its caller.
177The programmer-defined
178.BR matherr ()
179can modify this field to change the return value of the math function.
180
181If the
182.BR matherr ()
183function returns zero, then the system sets
184.I errno
185as described above, and may print an error message on standard error
186(see below).
187
188If the
189.BR matherr ()
c7094399 190function returns a nonzero value, then the system does not set
5f1f495c
MK
191.IR errno ,
192and doesn't print an error message.
193.SS Math functions that employ matherr()
194The table below lists the functions and circumstances in which
195.BR matherr ()
196is called.
197The "Type" column indicates the value assigned to
198.I exc\->type
199when calling
200.BR matherr ().
201The "Result" column is the default return value assigned to
202.IR exc\->retval .
203
204The "Msg?" and "errno" columns describe the default behavior if
205.BR matherr ()
206returns zero.
207If the "Msg?" columns contains "y",
208then the system prints an error message on standard error.
209
210The table uses the following notations and abbreviations:
211.RS
212.nf
213
214x first argument to function
215y second argument to function
216fin finite value for argument
217neg negative value for argument
218int integral value for argument
219o/f result overflowed
220u/f result underflowed
221|x| absolute value of x
222X_TLOSS is a constant defined in \fI<math.h>\fP
223.fi
224.RE
225.\" Details below from glibc 2.8's sysdeps/ieee754/k_standard.c
226.\" A subset of cases were test by experimental programs.
227.TS
228lB lB lB cB lB
229l l l c l.
230Function Type Result Msg? errno
231acos(|x|>1) DOMAIN HUGE y EDOM
232asin(|x|>1) DOMAIN HUGE y EDOM
233atan2(0,0) DOMAIN HUGE y EDOM
27844710
MK
234acosh(x<1) DOMAIN NAN y EDOM \" retval is 0.0/0.0
235atanh(|x|>1) DOMAIN NAN y EDOM \" retval is 0.0/0.0
236atanh(|x|==1) SING (x>0.0)? y EDOM \" retval is x/0.0
5f1f495c
MK
237\ \ HUGE_VAL :
238\ \ \-HUGE_VAL
239cosh(fin) o/f OVERFLOW HUGE n ERANGE
240sinh(fin) o/f OVERFLOW (x>0.0) ? n ERANGE
241\ \ HUGE : \-HUGE
242sqrt(x<0) DOMAIN 0.0 y EDOM
243hypot(fin,fin) o/f OVERFLOW HUGE n ERANGE
244exp(fin) o/f OVERFLOW HUGE n ERANGE
245exp(fin) u/f UNDERFLOW 0.0 n ERANGE
246exp2(fin) o/f OVERFLOW HUGE n ERANGE
247exp2(fin) u/f UNDERFLOW 0.0 n ERANGE
248exp10(fin) o/f OVERFLOW HUGE n ERANGE
249exp10(fin) u/f UNDERFLOW 0.0 n ERANGE
250j0(|x|>X_TLOSS) TLOSS 0.0 y ERANGE
251j1(|x|>X_TLOSS) TLOSS 0.0 y ERANGE
252jn(|x|>X_TLOSS) TLOSS 0.0 y ERANGE
253y0(x>X_TLOSS) TLOSS 0.0 y ERANGE
254y1(x>X_TLOSS) TLOSS 0.0 y ERANGE
255yn(x>X_TLOSS) TLOSS 0.0 y ERANGE
256y0(0) DOMAIN \-HUGE y EDOM
257y0(x<0) DOMAIN \-HUGE y EDOM
258y1(0) DOMAIN \-HUGE y EDOM
259y1(x<0) DOMAIN \-HUGE y EDOM
260yn(n,0) DOMAIN \-HUGE y EDOM
261yn(x<0) DOMAIN \-HUGE y EDOM
262lgamma(fin) o/f OVERFLOW HUGE n ERANGE
263lgamma(\-int) or SING HUGE y EDOM
264\ \ lgamma(0)
265tgamma(fin) o/f OVERFLOW HUGE_VAL n ERANGE
266tgamma(\-int) SING NAN y EDOM
267tgamma(0) SING copysign( y ERANGE
268\ \ HUGE_VAL,x)
269log(0) SING \-HUGE y EDOM
270log(x<0) DOMAIN \-HUGE y EDOM
271log2(0) SING \-HUGE n EDOM \" different from log()
c3074d70 272log2(x<0) DOMAIN \-HUGE n EDOM \" different from log()
5f1f495c
MK
273log10(0) SING \-HUGE y EDOM
274log10(x<0) DOMAIN \-HUGE y EDOM
275pow(0.0,0.0) DOMAIN 0.0 y EDOM
276pow(x,y) o/f OVERFLOW HUGE n ERANGE
277pow(x,y) u/f UNDERFLOW 0.0 n ERANGE
278pow(NaN,0.0) DOMAIN x n EDOM
2790**neg DOMAIN 0.0 y EDOM \" +0 and -0
280neg**non-int DOMAIN 0.0 y EDOM
281scalb() o/f OVERFLOW (x>0.0) ? n ERANGE
282\ \ HUGE_VAL :
283\ \ \-HUGE_VAL
284scalb() u/f UNDERFLOW copysign( n ERANGE
285\ \ \ \ 0.0,x)
286fmod(x,0) DOMAIN x y EDOM
27844710 287remainder(x,0) DOMAIN NAN y EDOM \" retval is 0.0/0.0
5f1f495c 288.TE
d79dfbf6 289.SH ATTRIBUTES
3c461ccc
PH
290For an explanation of the terms used in this section, see
291.BR attributes (7).
292.TS
293allbox;
294lb lb lb
295l l l.
296Interface Attribute Value
297T{
d79dfbf6 298.BR matherr ()
3c461ccc
PH
299T} Thread safety MT-Safe
300.TE
5f1f495c
MK
301.SH EXAMPLE
302The example program demonstrates the use of
303.BR matherr ()
304when calling
305.BR log (3).
306The program takes up to three command-line arguments.
307The first argument is the floating-point number to be given to
308.BR log (3).
309If the optional second argument is provided, then
310.B _LIB_VERSION
311is set to
312.B _SVID_
313so that
314.BR matherr ()
315is called, and the integer supplied in the
316command-line argument is used as the return value from
317.BR matherr ().
318If the optional third command-line argument is supplied,
319then it specifies an alternative return value that
320.BR matherr ()
321should assign as the return value of the math function.
322
323The following example run, where
324.BR log (3)
325is given an argument of 0.0, does not use
b18e759d 326.BR matherr ():
5f1f495c
MK
327
328.in +4n
329.nf
b43a3b30 330.RB "$" " ./a.out 0.0"
5f1f495c
MK
331errno: Numerical result out of range
332x=-inf
333.fi
334.in
335
336In the following run,
337.BR matherr ()
338is called, and returns 0:
339
340.in +4n
341.nf
b43a3b30 342.RB "$" " ./a.out 0.0 0"
5f1f495c
MK
343matherr SING exception in log() function
344 args: 0.000000, 0.000000
c3074d70 345 retval: \-340282346638528859811704183484516925440.000000
5f1f495c
MK
346log: SING error
347errno: Numerical argument out of domain
348x=-340282346638528859811704183484516925440.000000
349.fi
350.in
351
352The message "log: SING error" was printed by the C library.
353
354In the following run,
355.BR matherr ()
c7094399 356is called, and returns a nonzero value:
5f1f495c
MK
357
358.in +4n
359.nf
b43a3b30 360.RB "$" " ./a.out 0.0 1"
5f1f495c
MK
361matherr SING exception in log() function
362 args: 0.000000, 0.000000
c3074d70 363 retval: \-340282346638528859811704183484516925440.000000
5f1f495c
MK
364x=-340282346638528859811704183484516925440.000000
365.fi
366.in
367
368In this case, the C library did not print a message, and
369.I errno
370was not set.
371
372In the following run,
373.BR matherr ()
374is called, changes the return value of the math function,
c7094399 375and returns a nonzero value:
5f1f495c
MK
376
377.in +4n
378.nf
b43a3b30 379.RB "$" " ./a.out 0.0 1 12345.0"
5f1f495c
MK
380matherr SING exception in log() function
381 args: 0.000000, 0.000000
c3074d70 382 retval: \-340282346638528859811704183484516925440.000000
5f1f495c
MK
383x=12345.000000
384.fi
385.in
9c330504 386.SS Program source
d84d0300 387\&
5f1f495c
MK
388.nf
389#define _SVID_SOURCE
390#include <errno.h>
391#include <math.h>
392#include <stdio.h>
393#include <stdlib.h>
394
395static int matherr_ret = 0; /* Value that matherr()
396 should return */
397static int change_retval = 0; /* Should matherr() change
398 function\(aqs return value? */
399static double new_retval; /* New function return value */
400
401int
402matherr(struct exception *exc)
403{
404 fprintf(stderr, "matherr %s exception in %s() function\\n",
405 (exc\->type == DOMAIN) ? "DOMAIN" :
406 (exc\->type == OVERFLOW) ? "OVERFLOW" :
407 (exc\->type == UNDERFLOW) ? "UNDERFLOW" :
408 (exc\->type == SING) ? "SING" :
409 (exc\->type == TLOSS) ? "TLOSS" :
410 (exc\->type == PLOSS) ? "PLOSS" : "???",
411 exc\->name);
412 fprintf(stderr, " args: %f, %f\\n",
72da9ef1 413 exc\->arg1, exc\->arg2);
5f1f495c
MK
414 fprintf(stderr, " retval: %f\\n", exc\->retval);
415
416 if (change_retval)
417 exc\->retval = new_retval;
418
419 return matherr_ret;
420}
421
422int
423main(int argc, char *argv[])
424{
425 double x;
426
427 if (argc < 2) {
428 fprintf(stderr, "Usage: %s <argval>"
72da9ef1 429 " [<matherr\-ret> [<new\-func\-retval>]]\\n", argv[0]);
5f1f495c
MK
430 exit(EXIT_FAILURE);
431 }
432
433 if (argc > 2) {
434 _LIB_VERSION = _SVID_;
435 matherr_ret = atoi(argv[2]);
436 }
437
438 if (argc > 3) {
439 change_retval = 1;
440 new_retval = atof(argv[3]);
441 }
442
443 x = log(atof(argv[1]));
444 if (errno != 0)
445 perror("errno");
446
447 printf("x=%f\\n", x);
448 exit(EXIT_SUCCESS);
449}
450.fi
451.SH SEE ALSO
5f1f495c
MK
452.BR fenv (3),
453.BR math_error (7),
454.BR standards (7)