2 .\" Copyright (c) 2008, Linux Foundation, written by Michael Kerrisk
3 .\" <mtk.manpages@gmail.com>
5 .\" Permission is granted to make and distribute verbatim copies of this
6 .\" manual provided the copyright notice and this permission notice are
7 .\" preserved on all copies.
9 .\" Permission is granted to copy and distribute modified versions of this
10 .\" manual under the conditions for verbatim copying, provided that the
11 .\" entire resulting derived work is distributed under the terms of a
12 .\" permission notice identical to this one.
14 .\" Since the Linux kernel and libraries are constantly changing, this
15 .\" manual page may be incorrect or out-of-date. The author(s) assume no
16 .\" responsibility for errors or omissions, or for damages resulting from
17 .\" the use of the information contained herein. The author(s) may not
18 .\" have taken the same level of care in the production of this manual,
19 .\" which is licensed free of charge, as they might when working
22 .\" Formatted or processed versions of this manual, if unaccompanied by
23 .\" the source, must acknowledge the copyright and authors of this work.
25 .\"*******************************************************************
27 .\" This file was generated with po4a. Translate the source file.
29 .\"*******************************************************************
30 .TH MATHERR 3 2010\-09\-10 Linux "Linux Programmer's Manual"
32 matherr \- SVID math library exception handling
35 \fB#define _SVID_SOURCE\fP /* See feature_test_macros(7) */
36 \fB#include <math.h>\fP
38 \fBint matherr(struct exception *\fP\fIexc\fP\fB);\fP
40 \fBextern _LIB_VERSION_TYPE _LIB_VERSION;\fP
45 The System V Interface Definition (SVID) specifies that various math
46 functions should invoke a function called \fBmatherr\fP() if a math exception
47 is detected. This function is called before the math function returns;
48 after \fBmatherr\fP() returns, the system then returns to the math function,
49 which in turn returns to the caller.
51 The \fBmatherr\fP() mechanism is supported by glibc, but is now obsolete: new
52 applications should use the techniques described in \fBmath_error\fP(7) and
53 \fBfenv\fP(3). This page documents the glibc \fBmatherr\fP() mechanism as an aid
54 for maintaining and porting older applications.
56 To employ \fBmatherr\fP(), the programmer must define the \fB_SVID_SOURCE\fP
57 feature test macro (before including \fIany\fP header files), and assign the
58 value \fB_SVID_\fP to the external variable \fB_LIB_VERSION\fP.
60 The system provides a default version of \fBmatherr\fP(). This version does
61 nothing, and returns zero (see below for the significance of this). The
62 default \fBmatherr\fP() can be overridden by a programmer\-defined version,
63 which will be invoked when an exception occurs. The function is invoked
64 with one argument, a pointer to an \fIexception\fP structure, defined as
70 int type; /* Exception type */
71 char *name; /* Name of function causing exception */
72 double arg1; /* 1st argument to function */
73 double arg2; /* 2nd argument to function */
74 double retval; /* Function return value */
79 The \fItype\fP field has one of the following values:
82 A domain error occurred (the function argument was outside the range for
83 which the function is defined). The return value depends on the function;
84 \fIerrno\fP is set to \fBEDOM\fP.
87 A pole error occurred (the function result is an infinity). The return
88 value in most cases is \fBHUGE\fP (the largest single precision floating\-point
89 number), appropriately signed. In most cases, \fIerrno\fP is set to \fBEDOM\fP.
92 An overflow occurred. In most cases, the value \fBHUGE\fP is returned, and
93 \fIerrno\fP is set to \fBERANGE\fP.
96 An underflow occurred. 0.0 is returned, and \fIerrno\fP is set to \fBERANGE\fP.
99 Total loss of significance. 0.0 is returned, and \fIerrno\fP is set to
103 Partial loss of significance. This value is unused on glibc (and many other
106 The \fIarg1\fP and \fIarg2\fP fields are the arguments supplied to the function
107 (\fIarg2\fP is undefined for functions that take only one argument).
109 The \fIretval\fP field specifies the return value that the math function will
110 return to its caller. The programmer\-defined \fBmatherr\fP() can modify this
111 field to change the return value of the math function.
113 If the \fBmatherr\fP() function returns zero, then the system sets \fIerrno\fP as
114 described above, and may print an error message on standard error (see
117 If the \fBmatherr\fP() function returns a nonzero value, then the system does
118 not set \fIerrno\fP, and doesn't print an error message.
119 .SS "Math functions that employ matherr()"
120 The table below lists the functions and circumstances in which \fBmatherr\fP()
121 is called. The "Type" column indicates the value assigned to
122 \fIexc\->type\fP when calling \fBmatherr\fP(). The "Result" column is the
123 default return value assigned to \fIexc\->retval\fP.
125 The "Msg?" and "errno" columns describe the default behavior if \fBmatherr\fP()
126 returns zero. If the "Msg?" columns contains "y", then the system prints an
127 error message on standard error.
129 The table uses the following notations and abbreviations:
133 x first argument to function
134 y second argument to function
135 fin finite value for argument
136 neg negative value for argument
137 int integral value for argument
138 o/f result overflowed
139 u/f result underflowed
140 |x| absolute value of x
141 X_TLOSS is a constant defined in \fI<math.h>\fP
144 .\" Details below from glibc 2.8's sysdeps/ieee754/k_standard.c
145 .\" A subset of cases were test by experimental programs.
149 Function Type Result Msg? errno
150 acos(|x|>1) DOMAIN HUGE y EDOM
151 asin(|x|>1) DOMAIN HUGE y EDOM
152 atan2(0,0) DOMAIN HUGE y EDOM
153 .\" retval is 0.0/0.0
154 acosh(x<1) DOMAIN NAN y EDOM
155 .\" retval is 0.0/0.0
156 atanh(|x|>1) DOMAIN NAN y EDOM
158 atanh(|x|==1) SING (x>0.0)? y EDOM
161 cosh(fin) o/f OVERFLOW HUGE n ERANGE
162 sinh(fin) o/f OVERFLOW (x>0.0) ? n ERANGE
164 sqrt(x<0) DOMAIN 0.0 y EDOM
165 hypot(fin,fin) o/f OVERFLOW HUGE n ERANGE
166 exp(fin) o/f OVERFLOW HUGE n ERANGE
167 exp(fin) u/f UNDERFLOW 0.0 n ERANGE
168 exp2(fin) o/f OVERFLOW HUGE n ERANGE
169 exp2(fin) u/f UNDERFLOW 0.0 n ERANGE
170 exp10(fin) o/f OVERFLOW HUGE n ERANGE
171 exp10(fin) u/f UNDERFLOW 0.0 n ERANGE
172 j0(|x|>X_TLOSS) TLOSS 0.0 y ERANGE
173 j1(|x|>X_TLOSS) TLOSS 0.0 y ERANGE
174 jn(|x|>X_TLOSS) TLOSS 0.0 y ERANGE
175 y0(x>X_TLOSS) TLOSS 0.0 y ERANGE
176 y1(x>X_TLOSS) TLOSS 0.0 y ERANGE
177 yn(x>X_TLOSS) TLOSS 0.0 y ERANGE
178 y0(0) DOMAIN \-HUGE y EDOM
179 y0(x<0) DOMAIN \-HUGE y EDOM
180 y1(0) DOMAIN \-HUGE y EDOM
181 y1(x<0) DOMAIN \-HUGE y EDOM
182 yn(n,0) DOMAIN \-HUGE y EDOM
183 yn(x<0) DOMAIN \-HUGE y EDOM
184 lgamma(fin) o/f OVERFLOW HUGE n ERANGE
185 lgamma(\-int) or SING HUGE y EDOM
187 tgamma(fin) o/f OVERFLOW HUGE_VAL n ERANGE
188 tgamma(\-int) SING NAN y EDOM
189 tgamma(0) SING copysign( y ERANGE
191 log(0) SING \-HUGE y EDOM
192 log(x<0) DOMAIN \-HUGE y EDOM
193 .\" different from log()
194 log2(0) SING \-HUGE n EDOM
195 .\" different from log()
196 log2(x<0) DOMAIN \-HUGE n EDOM
197 log10(0) SING \-HUGE y EDOM
198 log10(x<0) DOMAIN \-HUGE y EDOM
199 pow(0.0,0.0) DOMAIN 0.0 y EDOM
200 pow(x,y) o/f OVERFLOW HUGE n ERANGE
201 pow(x,y) u/f UNDERFLOW 0.0 n ERANGE
202 pow(NaN,0.0) DOMAIN x n EDOM
204 0**neg DOMAIN 0.0 y EDOM
205 neg**non\-int DOMAIN 0.0 y EDOM
206 scalb() o/f OVERFLOW (x>0.0) ? n ERANGE
209 scalb() u/f UNDERFLOW copysign( n ERANGE
211 fmod(x,0) DOMAIN x y EDOM
212 .\" retval is 0.0/0.0
213 remainder(x,0) DOMAIN NAN y EDOM
216 The example program demonstrates the use of \fBmatherr\fP() when calling
217 \fBlog\fP(3). The program takes up to three command\-line arguments. The first
218 argument is the floating\-point number to be given to \fBlog\fP(3). If the
219 optional second argument is provided, then \fB_LIB_VERSION\fP is set to
220 \fB_SVID_\fP so that \fBmatherr\fP() is called, and the integer supplied in the
221 command\-line argument is used as the return value from \fBmatherr\fP(). If the
222 optional third command\-line argument is supplied, then it specifies an
223 alternative return value that \fBmatherr\fP() should assign as the return
224 value of the math function.
226 The following example run, where \fBlog\fP(3) is given an argument of 0.0,
227 does not use \fBmatherr\fP():
232 errno: Numerical result out of range
237 In the following run, \fBmatherr\fP() is called, and returns 0:
241 $\fB ./a.out 0.0 0\fP
242 matherr SING exception in log() function
243 args: 0.000000, 0.000000
244 retval: \-340282346638528859811704183484516925440.000000
246 errno: Numerical argument out of domain
247 x=\-340282346638528859811704183484516925440.000000
251 The message "log: SING error" was printed by the C library.
253 In the following run, \fBmatherr\fP() is called, and returns a nonzero value:
257 $\fB ./a.out 0.0 1\fP
258 matherr SING exception in log() function
259 args: 0.000000, 0.000000
260 retval: \-340282346638528859811704183484516925440.000000
261 x=\-340282346638528859811704183484516925440.000000
265 In this case, the C library did not print a message, and \fIerrno\fP was not
268 In the following run, \fBmatherr\fP() is called, changes the return value of
269 the math function, and returns a nonzero value:
273 $\fB ./a.out 0.0 1 12345.0\fP
274 matherr SING exception in log() function
275 args: 0.000000, 0.000000
276 retval: \-340282346638528859811704183484516925440.000000
289 static int matherr_ret = 0; /* Value that matherr()
291 static int change_retval = 0; /* Should matherr() change
292 function\(aqs return value? */
293 static double new_retval; /* New function return value */
296 matherr(struct exception *exc)
298 fprintf(stderr, "matherr %s exception in %s() function\en",
299 (exc\->type == DOMAIN) ? "DOMAIN" :
300 (exc\->type == OVERFLOW) ? "OVERFLOW" :
301 (exc\->type == UNDERFLOW) ? "UNDERFLOW" :
302 (exc\->type == SING) ? "SING" :
303 (exc\->type == TLOSS) ? "TLOSS" :
304 (exc\->type == PLOSS) ? "PLOSS" : "???",
306 fprintf(stderr, " args: %f, %f\en",
307 exc\->arg1, exc\->arg2);
308 fprintf(stderr, " retval: %f\en", exc\->retval);
311 exc\->retval = new_retval;
317 main(int argc, char *argv[])
322 fprintf(stderr, "Usage: %s <argval>"
323 " [<matherr\-ret> [<new\-func\-retval>]]\en", argv[0]);
328 _LIB_VERSION = _SVID_;
329 matherr_ret = atoi(argv[2]);
334 new_retval = atof(argv[3]);
337 x = log(atof(argv[1]));
341 printf("x=%f\en", x);
346 \fBfenv\fP(3), \fBmath_error\fP(7), \fBstandards\fP(7)