2 .\" Copyright (c) 2008, Linux Foundation, written by Michael Kerrisk
3 .\" <mtk.manpages@gmail.com>
5 .\" SPDX-License-Identifier: Linux-man-pages-copyleft
7 .TH matherr 3 (date) "Linux man-pages (unreleased)"
9 matherr \- SVID math library exception handling
12 .RI ( libm ", " \-lm )
17 .BI "[[deprecated]] int matherr(struct exception *" exc );
19 .B [[deprecated]] extern _LIB_VERSION_TYPE _LIB_VERSION;
23 the mechanism described in this page is no longer supported by glibc.
24 Before glibc 2.27, it had been marked as obsolete.
26 .\" glibc commit 813378e9fe17e029caf627cab76fe23eb46815fa
27 the mechanism has been removed altogether.
28 New applications should use the techniques described in
32 This page documents the
34 mechanism as an aid for maintaining and porting older applications.
36 The System V Interface Definition (SVID) specifies that various
37 math functions should invoke a function called
39 if a math exception is detected.
40 This function is called before the math function returns;
43 returns, the system then returns to the math function,
44 which in turn returns to the caller.
48 the programmer must define the
56 to the external variable
59 The system provides a default version of
61 This version does nothing, and returns zero
62 (see below for the significance of this).
65 can be overridden by a programmer-defined
66 version, which will be invoked when an exception occurs.
67 The function is invoked with one argument, a pointer to an
69 structure, defined as follows:
74 int type; /* Exception type */
75 char *name; /* Name of function causing exception */
76 double arg1; /* 1st argument to function */
77 double arg2; /* 2nd argument to function */
78 double retval; /* Function return value */
85 field has one of the following values:
88 A domain error occurred (the function argument was outside the range
89 for which the function is defined).
90 The return value depends on the function;
96 A pole error occurred (the function result is an infinity).
97 The return value in most cases is
99 (the largest single precision floating-point number),
100 appropriately signed.
107 An overflow occurred.
108 In most cases, the value
116 An underflow occurred.
123 Total loss of significance.
130 Partial loss of significance.
131 This value is unused on glibc
132 (and many other systems).
138 fields are the arguments supplied to the function
140 is undefined for functions that take only one argument).
144 field specifies the return value that the math
145 function will return to its caller.
146 The programmer-defined
148 can modify this field to change the return value of the math function.
152 function returns zero, then the system sets
154 as described above, and may print an error message on standard error
159 function returns a nonzero value, then the system does not set
161 and doesn't print an error message.
162 .SS Math functions that employ matherr()
163 The table below lists the functions and circumstances in which
166 The "Type" column indicates the value assigned to
170 The "Result" column is the default return value assigned to
173 The "Msg?" and "errno" columns describe the default behavior if
176 If the "Msg?" columns contains "y",
177 then the system prints an error message on standard error.
179 The table uses the following notations and abbreviations:
184 x first argument to function
185 y second argument to function
186 fin finite value for argument
187 neg negative value for argument
188 int integral value for argument
189 o/f result overflowed
190 u/f result underflowed
191 |x| absolute value of x
192 X_TLOSS is a constant defined in \fI<math.h>\fP
195 .\" Details below from glibc 2.8's sysdeps/ieee754/k_standard.c
196 .\" A subset of cases were test by experimental programs.
200 Function Type Result Msg? errno
201 acos(|x|>1) DOMAIN HUGE y EDOM
202 asin(|x|>1) DOMAIN HUGE y EDOM
203 atan2(0,0) DOMAIN HUGE y EDOM
204 acosh(x<1) DOMAIN NAN y EDOM \" retval is 0.0/0.0
205 atanh(|x|>1) DOMAIN NAN y EDOM \" retval is 0.0/0.0
206 atanh(|x|==1) SING (x>0.0)? y EDOM \" retval is x/0.0
209 cosh(fin) o/f OVERFLOW HUGE n ERANGE
210 sinh(fin) o/f OVERFLOW (x>0.0) ? n ERANGE
212 sqrt(x<0) DOMAIN 0.0 y EDOM
213 hypot(fin,fin) o/f OVERFLOW HUGE n ERANGE
214 exp(fin) o/f OVERFLOW HUGE n ERANGE
215 exp(fin) u/f UNDERFLOW 0.0 n ERANGE
216 exp2(fin) o/f OVERFLOW HUGE n ERANGE
217 exp2(fin) u/f UNDERFLOW 0.0 n ERANGE
218 exp10(fin) o/f OVERFLOW HUGE n ERANGE
219 exp10(fin) u/f UNDERFLOW 0.0 n ERANGE
220 j0(|x|>X_TLOSS) TLOSS 0.0 y ERANGE
221 j1(|x|>X_TLOSS) TLOSS 0.0 y ERANGE
222 jn(|x|>X_TLOSS) TLOSS 0.0 y ERANGE
223 y0(x>X_TLOSS) TLOSS 0.0 y ERANGE
224 y1(x>X_TLOSS) TLOSS 0.0 y ERANGE
225 yn(x>X_TLOSS) TLOSS 0.0 y ERANGE
226 y0(0) DOMAIN \-HUGE y EDOM
227 y0(x<0) DOMAIN \-HUGE y EDOM
228 y1(0) DOMAIN \-HUGE y EDOM
229 y1(x<0) DOMAIN \-HUGE y EDOM
230 yn(n,0) DOMAIN \-HUGE y EDOM
231 yn(x<0) DOMAIN \-HUGE y EDOM
232 lgamma(fin) o/f OVERFLOW HUGE n ERANGE
233 lgamma(\-int) or SING HUGE y EDOM
235 tgamma(fin) o/f OVERFLOW HUGE_VAL n ERANGE
236 tgamma(\-int) SING NAN y EDOM
237 tgamma(0) SING copysign( y ERANGE
239 log(0) SING \-HUGE y EDOM
240 log(x<0) DOMAIN \-HUGE y EDOM
241 log2(0) SING \-HUGE n EDOM \" different from log()
242 log2(x<0) DOMAIN \-HUGE n EDOM \" different from log()
243 log10(0) SING \-HUGE y EDOM
244 log10(x<0) DOMAIN \-HUGE y EDOM
245 pow(0.0,0.0) DOMAIN 0.0 y EDOM
246 pow(x,y) o/f OVERFLOW HUGE n ERANGE
247 pow(x,y) u/f UNDERFLOW 0.0 n ERANGE
248 pow(NaN,0.0) DOMAIN x n EDOM
249 0**neg DOMAIN 0.0 y EDOM \" +0 and -0
250 neg**non-int DOMAIN 0.0 y EDOM
251 scalb() o/f OVERFLOW (x>0.0) ? n ERANGE
254 scalb() u/f UNDERFLOW copysign( n ERANGE
256 fmod(x,0) DOMAIN x y EDOM
257 remainder(x,0) DOMAIN NAN y EDOM \" retval is 0.0/0.0
260 For an explanation of the terms used in this section, see
266 Interface Attribute Value
271 T} Thread safety MT-Safe
274 The example program demonstrates the use of
278 The program takes up to three command-line arguments.
279 The first argument is the floating-point number to be given to
281 If the optional second argument is provided, then
287 is called, and the integer supplied in the
288 command-line argument is used as the return value from
290 If the optional third command-line argument is supplied,
291 then it specifies an alternative return value that
293 should assign as the return value of the math function.
295 The following example run, where
297 is given an argument of 0.0, does not use
302 .RB "$" " ./a.out 0.0"
303 errno: Numerical result out of range
308 In the following run,
310 is called, and returns 0:
314 .RB "$" " ./a.out 0.0 0"
315 matherr SING exception in log() function
316 args: 0.000000, 0.000000
317 retval: \-340282346638528859811704183484516925440.000000
319 errno: Numerical argument out of domain
320 x=\-340282346638528859811704183484516925440.000000
324 The message "log: SING error" was printed by the C library.
326 In the following run,
328 is called, and returns a nonzero value:
332 .RB "$" " ./a.out 0.0 1"
333 matherr SING exception in log() function
334 args: 0.000000, 0.000000
335 retval: \-340282346638528859811704183484516925440.000000
336 x=\-340282346638528859811704183484516925440.000000
340 In this case, the C library did not print a message, and
344 In the following run,
346 is called, changes the return value of the math function,
347 and returns a nonzero value:
351 .RB "$" " ./a.out 0.0 1 12345.0"
352 matherr SING exception in log() function
353 args: 0.000000, 0.000000
354 retval: \-340282346638528859811704183484516925440.000000
360 .\" SRC BEGIN (matherr.c)
368 static int matherr_ret = 0; /* Value that matherr()
370 static int change_retval = 0; /* Should matherr() change
371 function\[aq]s return value? */
372 static double new_retval; /* New function return value */
375 matherr(struct exception *exc)
377 fprintf(stderr, "matherr %s exception in %s() function\en",
378 (exc\->type == DOMAIN) ? "DOMAIN" :
379 (exc\->type == OVERFLOW) ? "OVERFLOW" :
380 (exc\->type == UNDERFLOW) ? "UNDERFLOW" :
381 (exc\->type == SING) ? "SING" :
382 (exc\->type == TLOSS) ? "TLOSS" :
383 (exc\->type == PLOSS) ? "PLOSS" : "???",
385 fprintf(stderr, " args: %f, %f\en",
386 exc\->arg1, exc\->arg2);
387 fprintf(stderr, " retval: %f\en", exc\->retval);
390 exc\->retval = new_retval;
396 main(int argc, char *argv[])
401 fprintf(stderr, "Usage: %s <argval>"
402 " [<matherr\-ret> [<new\-func\-retval>]]\en", argv[0]);
407 _LIB_VERSION = _SVID_;
408 matherr_ret = atoi(argv[2]);
413 new_retval = atof(argv[3]);
416 x = log(atof(argv[1]));
420 printf("x=%f\en", x);