Syntax
#include <math.h> int _matherr(struct exception *x);Description
You can provide a different definition of _matherr to carry out special error handling. Be sure to use the /NOE link option, if you are using your own _matherr function. For The Developer's Toolkit compiler, you can use the /B option on the icc command line to pass the /NOE option to the linker, as in the following:
icc /B"/NOE" yourfile.c
When an error occurs in a math routine, _matherr is called with a pointer to the following structure (defined in <math.h>) as an argument:
struct exception { int type; char *name; double arg1, arg2, retval; };
The type field specifies the type of math error. It has one of the following values, defined in <math.h>: compact break=fit.
Value
PLOSS is provided for compatibility with other compilers; The Developer's Toolkit does not generate this value.
The name is a pointer to a null-ended string containing the name of the function that caused the error. The arg1 and arg2 fields specify the argument values that caused the error. If only one argument is given, it is stored in arg1. The retval is the default return value; you can change it.
The return value from _matherr must specify whether or not an error actually occurred. If _matherr returns 0, an error message appears, and errno is set to an appropriate error value. If _matherr returns a nonzero value, no error message appears and errno remains unchanged.
This example provides a _matherr function to handle errors from the log or log10 functions. The arguments to these logarithmic functions must be positive double values. _matherr processes a negative value in an argument (a domain error) by returning the log of its absolute value. It suppresses the error message normally displayed when this error occurs. If the error is a zero argument or if some other routine produced the error, the example takes the default actions.
#include <math.h>#include <string.h> #include <stdio.h> #include <stdlib.h> int main(void) { int value; printf("Trying to evaluate log10(-1000.00) will create a math exception.\n"); value = log10(-1000.00); printf("The _matherr() exception handler evaluates the expression to\n"); printf("log10(-1000.00) = %d\n", value); return 0; /**************************************************************************** The output should be: Trying to evaluate log10(-1000.00) will create a math exception. inside _matherr The _matherr() exception handler evaluates the expression to log10(-1000.00) = 3 ****************************************************************************/ } int _matherr(struct exception *x) { printf("inside _matherr\n"); if (DOMAIN == x->type) { if (0 == strcmp(x->name, "log")) { x->retval = log(-(x->arg1)); return EXIT_FAILURE; } else if (0 == strcmp(x->name, "log10")) { x->retval = log10(-(x->arg1)); return EXIT_FAILURE; } } return 0; /* Use default actions */ }Related Information