DOC HOME SITE MAP MAN PAGES GNU INFO SEARCH
 

matherr(S)


matherr, matherrl -- error-handling function

Syntax

   cc [flag ...] file ...  -lm [library ...]
   

#include <math.h>

int matherr(struct exception *x);

int matherrl(struct exceptionl *x);

Description

In programs compiled with the -Xt compilation option, matherr or matherrl (the long double version) is invoked by functions in the math libraries when errors are detected. Note that matherr or matherrl are not invoked when the -Xa and -Xc compilation options are used [see cc(CP)]. Users can define their own procedures for handling errors, by defining their own function named matherr or matherrl in their programs. The function must be of the form described above. When an error occurs, a pointer to the exception structure x is passed to the user-supplied matherr or matherrl function. 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 long double version is:
   struct exceptionl {
   	int type;
   	char *name;
   	long 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):

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

 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 incurred the error. The variables arg1 and arg2 are the arguments with which the function was invoked. retval is set to the default value that will be returned by the function unless the user's matherr function sets it to a different value.

If the user's matherr function returns non-zero, no error message will be printed, and errno will not be set.

If no matherr or matherrl function is supplied by the user, the default error-handling procedures, described with the math functions involved, will be invoked upon error.

Usage

The following definition of a matherr function performs some special case error handling:
   #include <math.h>
   #include <stdio.h>
   #include <stdlib.h>
   #include <string.h>
   

int matherr(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 TLOSS: /* 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 */ }

See also

cc(CP), math(M)

Notices

Error handling in -Xa, -Xc, and -Xt modes [see cc(CP)] is described more completely on individual math library pages.

Standards conformance

matherr is not part of any currently supported standard; it was developed by UNIX System Laboratories, Inc. and is maintained by The SCO Group.
© 2003 Caldera International, Inc. All rights reserved.
SCO OpenServer Release 5.0.7 -- 11 February 2003