matherr

Invoked each time an error is detected by functions in the math library

Local Servers:nonblocking
Remote Servers:N/A
Classification:ANSI
Service:Mathematical Computation

Syntax

  #include <math.h> 
    
  int matherr  (  
     struct exception    * err_info);
  

Parameters

err_info
(OUT) Points to the exception structure that contains information about the detected error.

Return Values

The matherr function returns zero when an error message is to be printed; otherwise it returns a nonzero value. The default matherr always returns zero.

Remarks

The matherr function is invoked each time an error is detected by functions in the math library. The default matherr supplied in the library returns zero, which causes an error message to be displayed upon stderr and errno to be set with an appropriate error value. An alternative version of this function can be provided, so that mathematical errors are handled by an application.

By calling RegisterMatherrHandler, you can provide a developer-written version of matherr to take any appropriate action when an error is detected. When zero is returned, an error message is printed upon stderr and errno is set as was the case with the default function. When a nonzero value is returned, no message is printed and errno is not changed. The value err_info -> retval is used as the return value for the function in which the error was detected.

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 */  
  };
  

See exception. Only retval can be changed by a developer-supplied version of matherr.

See Also

RegisterMatherrHandler

Example

  #include <math.h>  
  #include <string.h>  
  #include <stdio.h>  
   
  /*  
     Demonstrate error routine in which negative  
     arguments to "sqrt" are treated as positive  
  */  
  int main ()  
  {  
     RegisterMatherrHandler (altmatherr);  
     printf ("%e\n", sqrt (-5e0) );  
     exit ( 0 );  
  }  
   
  int altmatherr (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);  
  }