MATHERR(3M) — Silicon Graphics
NAME
matherr − error-handling function
SYNOPSIS
#include <math.h>
int matherr (x)
struct exception ∗x;
DESCRIPTION
Matherr is invoked by functions in the Math Library when errors are detected. Users may define their own procedures for handling errors by including a function named matherr in their programs. Matherr must be of the form described above. A pointer to the exception structure x will be passed to the user-supplied matherr function when an error occurs. This structure, which is defined in the <math.h> header file, is as follows:
struct exception {
int type;
char ∗name;
double arg1, arg2, retval;
};
The element type is an integer describing the type of error that has occurred, from the following list of constants (defined in the header file):
DOMAINdomain error
SINGsingularity
OVERFLOWoverflow
UNDERFLOW underflow
TLOSStotal loss of significance
PLOSSpartial loss of significance
The element name points to a string containing the name of the function that had the error. The variables arg1 and arg2 are the arguments to the function that had the error. Retval is a double that is returned by the function having the error. If it supplies a return value, the user’s matherr must return non-zero. If the default error value is to be returned, the user’s matherr must return 0.
If matherr is not supplied by the user, the default error-handling procedures, described with the math functions involved, will be invoked upon error. These procedures are also summarized in the table below. In every case, errno is set to non-zero and the program continues.
EXAMPLE
matherr(x)
register struct exception ∗x;
{
switch (x−>type) {
case DOMAIN:
case SING: /∗ print message and abort ∗/
fprintf(stderr, "domain error in %s\n", x−>name);
abort( );
case OVERFLOW:
if (!strcmp("exp", x−>name)) {
/∗ if exp, print message, return the argument ∗/
fprintf(stderr, "exp of %f\n", x−>arg1);
x−>retval = x−>arg1;
} else if (!strcmp("sinh", x−>name)) {
/∗ if sinh, set errno, return 0 ∗/
errno = ERANGE;
x−>retval = 0;
} else
/∗ otherwise, return HUGE ∗/
x−>retval = HUGE;
break;
case UNDERFLOW:
return (0); /∗ execute default procedure ∗/
case TLOSS:
case PLOSS:
/∗ print message and return 0 ∗/
fprintf(stderr, "loss of significance in %s\n", x−>name);
x−>retval = 0;
break;
}
return (1);
}
| DEFAULT ERROR HANDLING PROCEDURES | ||||||
| Types of Errors | ||||||
| DOMAIN | SING | OVERFLOW | UNDERFLOW | TLOSS | PLOSS | |
| BESSEL: | − | − | H | 0 | − | ∗ |
| y0, y1, yn | M, −H | − | − | − | − | − |
| (neg. no.) | ||||||
| EXP: | − | − | H | 0 | − | |
| POW: | − | − | H | 0 | − | − |
| (neg.)∗∗(non- | M, 0 | − | − | − | − | − |
| int.), 0∗∗0 | ||||||
| LOG: | ||||||
| log(0): | − | M, −H | − | − | − | − |
| log(neg.): | M, −H | − | − | − | − | − |
| SQRT: | M, 0 | − | − | − | − | − |
| GAMMA: | − | M, H | − | − | − | − |
| HYPOT: | − | − | H | − | − | − |
| SINH, COSH: | − | − | H | − | − | − |
| SIN, COS: | − | − | − | − | M, 0 | M, ∗ |
| TAN: | − | − | H | − | 0 | ∗ |
| ACOS, ASIN: | M, 0 | − | − | − | − | − |
| ABBREVIATIONS | |
| ∗ | As much as possible of the value is returned. |
| M | Message is printed. |
| H | HUGE is returned. |
| −H | −HUGE is returned. |
| 0 | 0 is returned. |
Version 2.5 — April 22, 1987