matherr, _matherrl
User-modifiable math error handler.
Defined in header <math.h>
Syntax
#include <math.h>
int matherr(struct exception *e);
int _matherrl(struct exceptionl *e);
Portability
| DOS | UNIX | Windows | ANSI C | C++ only |
|---|---|---|---|---|
| ■ | ■ |
Remarks
matherr is called when an error is generated by the math library.
_matherrl is the long double version; it is called when an error is generated by the long double math functions.
matherr and _matherrl each serve as a user hook (a function that can be customized by the user) that you can replace by writing your own math error handling routine — see the following example of a user-defined matherr implementation.
matherr and _matherrl are useful for trapping domain and range errors caused by the math functions. They do not trap floating-point exceptions, such as division by zero. See signal for trapping such errors.
You can define your own matherr or _matherrl routine to be a custom error handler (such as one that catches and resolves certain types of errors); this customized function overrides the default version in the C library. The customized matherr or _matherrl should return 0 if it fails to resolve the error, or nonzero if the error is resolved. When matherr or _matherrl return nonzero, no error message is printed and the global variable errno is not changed.
Here are the exception and _exceptionl structures (defined in math.h):
struct exception { int type; char *Function; double argl, arg2, retval; }; struct _exceptionl { int type; char *Function; long double argl, arg2, retval; };■ The members of the exception and _exceptionl structures are shown in the following table:
Member What it is (or represents)
| type | The type of mathematical error that occurred; an enum type defined in the typedef jnexcep (see definition after this list). |
| name | A pointer to a null-terminated string holding the name of the math library function that resulted in an error |
| argl, | The arguments (passed to the function name points to) that |
| arg2 | caused the error; if only one argument was passed to the function, it is stored in argl. |
| retval | The default return value for matherr (or _matherrl); you can modify this value. |
The typedef jnexcep, also defined in math.h, enumerates the following symbolic constants representing possible mathematical errors:
Symbolic
| constant | l\/!athematical error |
| DOMAIN | Argument was not in domain of function, such as log(-l). |
| SING | Argument would result in a singularity, such as pow(0, -2). |
OVERFLOW Argument would produce a function result greater than DBL_MAX (or LDBL_MAX), such as exp(lOOO). UNDERFLOW Argument would produce a function result less than DBL_M1N (or LDBL_M1N), such as exp(-lOOO).
| TLOSS | Argument would produce function result with total loss of significant digits, such assin(10e70). |
| The macros DBL_MAX, | DBL_MIN, | LDBL_MAX, | and LDBL_MIN | are |
defined in float.h.
The source code to the default nnatherr and _matherrl is on the Borland C++ distribution disks.
The UNIX-style matherr and _matherrl default behavior (printing a message and terminating) is not ANSI compatible. If you desire a UNIX-
| style version of these routines, use MATHERR.C | and MATHERRL.C |
provided on the Borland C++ distribution disks.
Return value
The default return value for matherr and _matherrl is 1 if the error is
| UNDERFLOW | or TLOSS, 0 otherwise, matherr and _matherrl can also |
modify e -> retval, which propagates back to the original caller.
When matherr and _matherrl return 0 (indicating that they were not able to resolve the error), the global variable errno is set to 0 and an error message is printed.
When matherr and _matherrl return nonzero (indicating that they were able to resolve the error), the global variable errno is not set and no messages are printed.
Example
#include <math.h>
#include <string.h>
#include <stdio.h>
int matherr (struct exception *a)
{
if (a->type == DOMAIN)
if (!strcmp(a->name, "sqrt")) {
a->retval = sqrt (-(a->argl));
return 1;
}
return 0;
}
int main (void)
{
double X = -2.0, y;
y = sqrt (x);
printf ("Matherr corrected value: %lf\n",y);
return 0;
Differences from modern implementations
Nothing recorded yet.
Modern references
These are search links, not yet checked by hand.
Extraction notes
- heading `matherr, matherri` read as `matherr, _matherrl`
Stable link: /3.1/math.h/matherr/
· short form /3.1/matherr/