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