display error messages for math library functions
#include <math.h> int matherr( struct exception *err_info );
The matherr() function is invoked each time an error is detected by functions in the math library. The default matherr() function supplied in the library returns zero, which causes:
An alternative version of this function can be provided, instead of the library version, in order that the error handling for mathematical errors can be handled by an application.
A program may contain a user-written version of matherr() to take any appropriate action when an error is detected. For example, the replacement function could do the following:
The matherr() function is passed a pointer to a structure of type struct exception, which contains information about the error that has been detected:
struct exception { int type; /* type of error */ char *name; /* name of function */ double arg1; /* first argument to function */ double arg2; /* second argument to function */ double retval; /* default return value */ };
The type field contains one of the following values:
The name field points to a string containing the name of the function that detected the error. The fields arg1 and arg2 (if required) give the values that caused the error. The field retval contains the value that will be returned by the function. This value may be changed by a user-supplied version of the matherr() function.
Zero when an error message is to be printed, and a nonzero value otherwise.
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <math.h> /* Demonstrate an error routine in which negative */ /* arguments to "sqrt" are treated as positive */ int main( void ) { printf( "%e\n", sqrt( -5e0 ) ); return( EXIT_SUCCESS ); } int matherr( struct exception *err ) { if( strcmp( err->name, "sqrt" ) == 0 ) { if( err->type == DOMAIN ) { err->retval = sqrt( -(err->arg1) ); return( 1 ); } else return( 0 ); } else return( 0 ); }
WATCOM
Safety: | |
---|---|
Interrupt handler | No |
Signal handler | No |
Thread | No |