Retro video games delivered to your door every month!
Click above to get retro games delivered to your door ever month!
X-Hacker.org- Turbo C - <b>matherr() handle math error</b> http://www.X-Hacker.org [<<Previous Entry] [^^Up^^] [Next Entry>>] [Menu] [About The Guide]
matherr()                Handle Math Error

 #include   <math.h>

 int               matherr(x);
 struct exception  *x;                   Math exception information

    matherr() is called when errors are detected by the math functions.
    (See the individual math functions for specific information on when
    matherr() is called.)  A program can supply its own matherr()
    function to provide special error handling.

    When an error in a math function occurs, matherr() is called with a
    pointer to an exception structure, 'x', that holds the error
    information. The structure 'exception' is defined in math.h as:

          struct exception
                 {
                  int      type;        /* type of math error */
                  char     *name;       /* name of function causing error */
                  double   arg1, arg2;  /* argument(s) of math function */
                  double   retval;      /* default error return value */
                 };

    'type' specifies the type of math error and is one of the following
    values (defined in <math.h>):

                DOMAIN        Argument domain error
                SING          Argument singularity
                OVERFLOW      Overflow range error
                UNDERFLOW     Underflow range error
                PLOSS         Partial loss of significance
                TLOSS         Total loss of significance

    'name' is a pointer to a null-terminated string containing the name
    of the math function that caused the error; 'arg1' (and 'arg2' if the
    function accepts two arguments) are the argument(s) that caused the
    error; and 'retval' is the value that will be returned by the
    function for this error.  'retval' may be changed by matherr(), thus
    changing the value that will be returned by the math function. (Be
    careful:  The return value is used to specify that an error
    occurred.)

    matherr() returns 0 to indicate that an error still exists, in which
    case the math function will print any error messages to 'stderr' and
    set 'errno' (defined in stdlib.h) to the appropriate error value.
    matherr() returns a non-zero value to indicate that the error has
    been corrected, in which case the function should suppress error
    messages and should not set 'errno'.

    Returns:    0 to indicate that the error still exists and non-zero to
                indicate that the error has been corrected.

      Notes:    The default matherr() error handler just returns 0.

                The following functions call matherr(): acos, asin, atan,
                atan2, bessel, cabs, cos, cosh, exp, hypot, log, pow,
                sin, sinh, sqrt, and tan.

  -------------------------------- Example ---------------------------------

    The following statements demonstrate how you can use the matherr()
    function to trap certain errors from specific functions; this example
    traps two errors from the pow() function.  pow() errors are trapped
    so that calls to pow() with 0.0 as the first argument and a negative
    second argument will return 0.0 and suppress the error message; calls
    with a negative first argument and a non-integer second argument are
    re-evaluated with the floor() of the second argument. The default
    actions are taken for all other pow() errors, and all errors
    generated by other routines:

         #include <math.h> /* for pow(), floor(), DOMAIN, struct exception */
         #include <string.h> /* for strcmp() */

         int matherr(x)
         struct exception *x;
         {
             if (x->type == DOMAIN  &&  strcmp(x->name, "pow") == 0) {
                 if (x->arg1 == 0.0  &&  x->arg2 < 0.0)
                     x->retval = 0.0;
                 else if (x->arg1 < 0.0)  /* negative 1st arg... */
                     x->retval = pow(x->arg1, floor(x->arg2));
                 return (1);
             }
             return (0);    /* use default actions */
         }

         main()
         {
             double x;

             x = pow(5.5, 10.0);   /* no errors, matherr() not called */
             x = pow(0.0, -1.0);   /* 0.0 returned by matherr(),
                                      errno not set, no error messages */
             x = pow(-5, 4.2);     /* pow(-5, 4.0) returned by matherr(),
                                      errno not set, no error messages */
             x = pow(7.7, 1000.0); /* OVERFLOW error, matherr() called,
                                      default error actions taken */
         }

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