matherr [ HP C/iX Library Reference Manual ] MPE/iX 5.0 Documentation
HP C/iX Library Reference Manual
matherr
The matherr function is a user-written call-back routine invoked by many
functions in the math library when errors are detected.
Syntax
#include <math.h>
int matherr (x)
struct exception *x;
Parameters
x A pointer to the exception structure defined in the
<math.h> header file.
Return Values
x A user-defined integer value.
Description
Users override the default math library error handler by defining a
function named matherr in their programs. This user-written matherr
function must follow the syntax described above.
When an error occurs, a pointer to the exception structure x is passed to
your matherr function.
The structure is defined as follows:
struct exception {
int type;
char *name;
double arg1, arg2, retval;
};
The element type is an integer describing the type of error that
occurred, from the following list of constants defined in the header
file:
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 element name points to a string containing the name of the function
that caused the error. The variables arg1 and arg2 are the arguments you
use to invoke the function. retval is set to the default value that is
returned by the function unless your matherr sets it to a different
value.
Consult the function descriptions in this chapter to determine if a
specific function calls matherr.
If your matherr function returns nonzero, no error message is printed,
and errno is not set.
If matherr is not supplied, the default error-handling procedures,
described with the math functions involved, are invoked upon error. In
every case, errno is set to EDOM or ERANGE, and the program continues.
Example
#include <math.h>
int matherr(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 */
(void) fprintf(stderr, "domain error in %s\n", x->name);
abort( );
case PLOSS:
/* print detailed error message */
(void) 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 */
}
MPE/iX 5.0 Documentation