Retro video games delivered to your door every month!
Click above to get retro games delivered to your door ever month!
X-Hacker.org- Watcom C Library Reference - <u>synopsis:</u> http://www.X-Hacker.org [<<Previous Entry] [^^Up^^] [Next Entry>>] [Menu] [About The Guide]
Synopsis:
    #include <math.h>
    void _set_matherr( int (*rtn)( struct exception *err_info ) )

Description:
    The default  matherr function supplied in the library can be replaced so
    that the application can handle mathematical errors.  To do this, the
    _set_matherr function must be called with the address of the new
    mathematical error handling routine.

    Note:  Under some systems, the default math error handler can be
    replaced by providing a user-written function of the same name,
     matherr, and using linking strategies to replace the default handler.
     Under PenPoint, the default handler is bound into a dynamic link
    library and can only be replaced by notifying the C library with a call
    to the _set_matherr function.

    A program may contain a user-written version of  matherr to take any
    appropriate action when an error is detected.  When zero is returned by
    the user-written routine, an error message will be printed upon  stderr
    and  errno will be set as was the case with the default function.  When
    a non-zero 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.

    When called, the user-written math error handler 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 will contain one of the following values:

    Value     Meaning

DOMAIN
    A domain error has occurred, such as sqrt(-1e0).

SING
    A singularity will result, such as pow(0e0,-2).

OVERFLOW
    An overflow will result, such as pow(10e0,100).

UNDERFLOW
    An underflow will result, such as pow(10e0,-100).

TLOSS
    Total loss of significance will result, such as exp(1000).

PLOSS
    Partial loss of significance will result, such as sin(10e70).

    The name field points to a string containing the name of the function
    which detected the error.  The fields arg1 and arg2 (if required) give
    the values which caused the error.  The field retval contains the value
    which will be returned by the function.  This value may be changed by a
    user-supplied version of the _set_matherr function.

Returns:
    The _set_matherr function returns no value.

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

    /* Demonstrate error routine in which negative */
    /* arguments to "sqrt" are treated as positive */

    static int my_matherr( struct exception *err );

    void main()
      {
        _set_matherr( &my_matherr );
        printf( "%e\n", sqrt( -5e0 ) );
        exit( 0 );
      }

    int my_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 );
      }

Classification:
    WATCOM

Systems:
    Math

Online resources provided by: http://www.X-Hacker.org --- NG 2 HTML conversion by Dave Pearson