matherr(3) — Subroutines
Digital
NAME
matherr − error-handling function for the SVID-2 habitat math library
SYNOPSIS
#include <math.h>
int matherr (x)
struct exception ∗x;
PARAMETERS
xException structure.
DESCRIPTION
You can override the default error-handling procedures for the SVID-2 habitat math library functions by defining a matherr() function in your programs. The matherr() function is valid only in the SVID-2 habitat, and you must link to the SVID-2 habitat math library to use this function. The SVID-2 habitat is a subset of commands, subroutines, and system calls that conforms to the System V Interface Definition (SVID-2).
When an error occurs and a user-supplied matherr() function is not found, the default error-handling procedures, which are described the reference page for each math function, are used. In every case, errno is set to EDOM or ERANGE and the program continues.
When an error occurs and a user-supplied matherr() function is defined, a pointer to the x exception structure is passed to the matherr function. This structure, which is defined in the <math.h> header file, has the following form: struct exception {
int type;
char ∗name;
double arg1, arg2, retval;
};
The type argument is an integer describing the error that has occurred from the following list of constants:
| Constant | Description |
| DOMAIN | argument domain error |
| SING | argument singularity |
| OVERFLOW | overflow range error |
| UNDERFLOW | underflow range error |
| TLOSS | total loss of significance |
| PLOSS | partial loss of significance |
The name argument points to a string containing the name of the function that incurred the error. The variables arg1 and arg2 are the arguments with which the function was invoked. The retval argument is set to the default value that is returned by the function unless the user’s matherr sets it to a different value.
If the user’s matherr function returns non-zero, no error message is printed, and errno is not set.
EXAMPLE
#include <math.h>
int
matherr(x)
register struct exception ∗x;
{
switch (x−>type) {
case DOMAIN:
/∗ change sqrt to return
sqrt(−arg1), not 0 ∗/
if (!strcmp(x−>name, "sqrt")) {
x−>retval = sqrt(−x−>arg1);
return (0);
/∗ print message and set errno ∗/
}
case SING:
/∗ all other domain or sing errors,
print message and abort ∗/
fprintf(stderr, "domain error in %s\n",
x−>name);
abort( );
case PLOSS:
/∗ print detailed error message ∗/
fprintf(stderr,
"loss of significance in %s(%g) = %g\n",
x−>name, x−>arg1, x−>retval);
return (1); /∗ take no other action ∗/
}
return (0); /∗ all other errors, execute default
procedure ∗/
}
NOTES
To use matherr(), you must link to the SVID-2 habitat math library.
RELATED INFORMATION
Functions: bessel(3), exp(3), gamma(3), hypot(3), sin(3), sinh(3), sqrt(3).
System V Compatibility User’s Guide