Handle math errors.
#include <math.h>
int _matherr( struct _exception *except );
int _matherrl( struct _exceptionl *except );
except | Pointer to structure containing error information |
The _matherr functions process errors generated by the functions of the math library. The math functions call the appropriate _matherr routine whenever an error is detected. The _matherrl function uses the 80-bit, 10-byte coprocessor form of arguments and return values. See the reference page on the long double functions for more details on this data type.
The user can provide a different definition of the _matherr or _matherrl function to carry out special error handling.
When an error occurs in a math routine, _matherr is called with a pointer to an _exception type structure (defined in MATH.H) as an argument.
The _exception structure contains the following elements:
Element | Description |
int type | Exception type |
char *name | Name of function where error occurred |
double arg1, arg2 | First and second (if any) argument to function |
double retval | Value to be returned by function |
The type specifies the type of math error. It is one of the following values, defined in MATH.H:
Value | Meaning |
_DOMAIN | Argument domain error |
_SING | Argument singularity |
_OVERFLOW | Overflow range error |
_PLOSS | Partial loss of significance |
_TLOSS | Total loss of significance |
_UNDERFLOW | Underflow range error |
The structure member name is a pointer to a null-terminated string containing the name of the function that caused the error. The structure members arg1 and arg2 specify the values that caused the error. (If only one argument is given, it is stored in arg1.)
The default return value for the given error is retval. If you change the return value, remember that the return value must specify whether an error actually occurred. If the _matherr function returns 0, an error message can be displayed and errno is set to an appropriate error value. If _matherr returns a nonzero value, no error message is displayed, and errno remains unchanged.
The _matherr functions should return 0 to indicate an error, and a nonzero value to indicate successful corrective action.
_matherr
Standards:UNIX
16-Bit:DOS, QWIN, WIN, WIN DLL
32-Bit:DOS32X
Use _matherr for compatibility with ANSI naming conventions of non-ANSI functions. Use matherr and link with OLDNAMES.LIB for UNIX compatibility.
_matherrl
Standards:None
16-Bit:DOS, QWIN, WIN, WIN DLL
32-Bit:None
acos functions, asin functions, atan functions, bessel functions, _cabs, cos functions, exp, _hypot, log functions, pow, sin functions, sqrt, tan functions
/* MATHERR.C: To use _matherr, you must turn off the Extended Dictionary
* flag within the Microsoft Programmer's WorkBench environment, or use the
* /NOE linker option outside the environment. For example:
* CL _matherr.c /link /NOE
*/
#include <math.h>
#include <string.h>
#include <stdio.h>
void main( void )
{
/* Do several math operations that cause errors. The _matherr
* routine handles _DOMAIN errors, but lets the system handle
* other errors normally.
*/
printf( "log( -2.0 ) = %e\n", log( -2.0 ) );
printf( "log10( -5.0 ) = %e\n", log10( -5.0 ) );
printf( "log( 0.0 ) = %e\n", log( 0.0 ) );
}
/* Handle several math errors caused by passing a negative argument
* to log or log10 (_DOMAIN errors). When this happens, _matherr returns
* the natural or base-10 logarithm of the absolute value of the
* argument and suppresses the usual error message.
*/
int _matherr( struct _exception *except )
{
/* Handle _DOMAIN errors for log or log10. */
if( except->type == _DOMAIN )
{
if( strcmp( except->name, "log" ) == 0 )
{
except->retval = log( -(except->arg1) );
printf( "Special: using absolute value: %s: _DOMAIN error\n",
except->name );
return 1;
}
else if( strcmp( except->name, "log10" ) == 0 )
{
except->retval = log10( -(except->arg1) );
printf( "Special: using absolute value: %s: _DOMAIN error\n",
except->name );
return 1;
}
}
else
{
printf( "Normal: " );
return 0; /* Else use the default actions */
}
}
Special: using absolute value: log: _DOMAIN error
log( -2.0 ) = 6.931472e-001
Special: using absolute value: log10: _DOMAIN error
log10( -5.0 ) = 6.989700e-001
Normal: log: _SING error
log( 0.0 ) = -1.797693e+308