Bewerken

Delen via


_matherr

Handles math errors.

Syntax

int _matherr(struct _exception *except);

Parameters

except
Pointer to the structure containing error information.

Return value

_matherr returns 0 to indicate an error, or a nonzero value to indicate success:

  • If _matherr 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.

For more information about return codes, see errno, _doserrno, _sys_errlist, and _sys_nerr.

Remarks

The _matherr function processes errors generated by the floating-point functions of the math library. These functions call _matherr when an error is detected. This interaction isn't impacted by the floating-point mode of the compiler or the floating point control word. Since _matherr is a library function, math intrinsic functions won't call it.

For special error handling, you can provide a different definition of _matherr. If you use the dynamically linked version of the C run-time library (CRT), you can replace the default _matherr routine in a client executable with a user-defined version. However, you can't replace the default _matherr routine in a DLL client of the CRT DLL.

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.

struct _exception
{
    int    type;   // exception type - see below
    char*  name;   // name of function where error occurred
    double arg1;   // first argument to function
    double arg2;   // second argument (if any) to function
    double retval; // value to be returned by function
};

The type member specifies the type of math error. It's one of the following values, defined in <math.h>:

Macro Description
_DOMAIN Argument domain error
_SING Argument singularity
_OVERFLOW Overflow range error
_PLOSS Partial loss of significance
_TLOSS Total loss of significance
_UNDERFLOW The result is too small to be represented. (This condition isn't currently supported.)

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's stored in arg1.

The default return value for the given error is retval. If you change the return value, it must specify whether an error actually occurred.

Requirements

Routine Required header
_matherr <math.h>

For more compatibility information, see Compatibility.

Example

/* crt_matherr.c
* Illustrates writing an error routine for math
* functions. 
* The error handling function must be named _matherr
*/

#include <math.h>
#include <string.h>
#include <stdio.h>

int main()
{
    /* 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;
        }
    }
    printf("Normal: ");
    return 0;    /* Else use the default actions */
}
Special: using absolute value: log: _DOMAIN error
log( -2.0 ) = 6.931472e-01
Special: using absolute value: log10: _DOMAIN error
log10( -5.0 ) = 6.989700e-01
Normal: log( 0.0 ) = -inf

See also

Math and floating-point support