matherr(3M)

matherr - error-handling function

As shipped in IRIX 6.5. First release of IRIX 6.5.

NAME
     matherr - error-handling function

SYNOPSIS
     #include <math.h>

     int matherr (struct exception *x);

DESCRIPTION
     matherr is invoked by the following functions in libmx.a when errors are
     detected:

     acos acosf acosh asin asinf asinh atan atanf atan2 atan2f atanh cbrt cos
     cosf cosh coshf drem erf erfc
     exp expf expm1 expm1f fmod fmodf gamma hypot hypotf j0 j1 jn lgamma log
     logf log10 log10f log1p log1pf
     pow powf sin sinf sinh sinhf sqrt sqrtf tan tanf tanh tanhf y0 y1 yn

     Note that to use matherr, libmx.a needs to be linked with the program.
     Users may define their own routines for handling errors by including a
     function named matherr in their programs.  matherr must be of the form
     described above.  When an error occurs, a pointer to the exception
     structure x will be passed to the user-supplied matherr function.  This
     structure, which is defined in the math.h header file, is as follows:

          struct exception {
               int type;
               char *name;
               double arg1, arg2, retval;
          };

     The element type is an integer describing the type of error that has
     occurred, from the following list of constants (defined in the header
     file math.h):

          DOMAIN      argument domain error
          SING        argument singularity
          OVERFLOW    overflow range error
          UNDERFLOW   underflow range error
          TLOSS       total loss of significance
          PLOSS       partial loss of significance

     The element name points to a string containing the name of the function
     that incurred the error.  The elements arg1 and arg2 are the arguments
     with which the function was invoked.  retval is set to the default value
     that will be returned by the function unless the user's matherr sets it
     to a different value.

     NaN argument(s) to any of the above functions generates a call to matherr
     with a domain error.

     If the user's matherr function returns non-zero, no error message will be
     printed by the calling function, and errno will not be set.

     If matherr is not supplied by the user, the default error-handling
     procedures, described by the table below, will be invoked upon error.  In
     every case, errno is set to EDOM or ERANGE and the program continues.

_______________________________________________________________________________
                      Default Error Handling Procedures
______________________________________________________________________________
                                          Types of Errors
______________________________________________________________________________
        type          DOMAIN   SING   OVERFLOW    UNDERFLOW   TLOSS    PLOSS
______________________________________________________________________________
       errno           EDOM    EDOM    ERANGE      ERANGE     ERANGE   ERANGE
______________________________________________________________________________
 EXP, EXPF:             -       -       M, H        M, 0        -        -
______________________________________________________________________________
 EXPM1, EXPM1F:         -       -       M, H        M, 0        -        -
______________________________________________________________________________
 LOG, LOGF:
 LOG10, LOG10F:
  (arg < 0)            M, N     -        -            -         -        -
  (arg = 0)            M, -H    -        -            -         -        -
______________________________________________________________________________
 LOG1P, LOG1PF:
  (arg < -1)           M, N     -        -            -         -        -
  (arg = -1)           M, -H    -        -            -         -        -
______________________________________________________________________________
 POW, POWF:             -       -        _H           0         -        -
 neg ** non-int        M, 0     -        -            -         -        -
   0 ** non-pos        M, H     -        -            -         -        -
______________________________________________________________________________
 SQRT, SQRTF:          M, N     -        -            -         -        -
______________________________________________________________________________
 DREM, FMOD, FMODF:
  (arg1 = +/-inf       M, N     -        -            -         -        -
   or arg2 = 0)
______________________________________________________________________________
 GAMMA, LGAMMA:         -      M, H     M, H          -         -        -
______________________________________________________________________________
 J0, J1, JN:            -       -        -            -        M, 0      -
______________________________________________________________________________
 Y0, Y1, YN:
  (arg = 0)             -       -      M, -H          -         -        -
  (arg < 0)            M, N     -        -            -         -        -
  (arg >= 2**49)        -       -        -            -        M, 0      -
______________________________________________________________________________
 SINH, SINHF:           -       -        _H           -         -        -
______________________________________________________________________________
 COSH, COSHF:           -       -        H            -         -        -
______________________________________________________________________________
 HYPOT, HYPOTF:         -       -       M, H          -         -        -
______________________________________________________________________________
 ASIN, ASINF:
 ACOS, ACOSF:          M, N     -        -            -         -        -
______________________________________________________________________________
______________________________________________________________________________
|
















































                     |














































                              |












































                                     |












































                                                |












































                                                            |












































                                                                     |












































                                                                              |

 SIN, COS, TAN:
  (arg >= 2**50)       M, N     -        -            -         -        -
  (arg = +/-inf)        -       -        -            -        M, 0      -
______________________________________________________________________________
 SINF, COSF, TANF:
  (arg >= 2**22)       M, N     -        -            -         -        -
  (arg = +/-inf)        -       -        -            -        M, 0      -
______________________________________________________________________________
 ATAN2, ATAN2F:        M, 0     -        -            -         -        -
______________________________________________________________________________
 ACOSH:                M, N     -        -            -         -        -
______________________________________________________________________________
 ATANH:
  (|arg| > 1)          M, N     -        -            -         -        -
______________________________________________________________________________
|













                     |












                              |












                                     |












                                                |












                                                            |












                                                                     |












                                                                              |














______________________________________________________________________________
                                Abbreviations
          M                               Message is printed
          H                               HUGE is returned
          -H                              -HUGE is returned
          _H                              HUGE or -HUGE is returned.
          0                               0 is returned.
          X                               arg1 is returned.
          N                               NaN is returned.
______________________________________________________________________________
|







                                                                              |

EXAMPLE
     /*
     ** Example showing the use of libmx.a's matherr() to handle math errors.
     **
     **  % cc -xansi my_matherr.c -lmx
     **  % a.out
     **  overflow range error in logf
     **  logf(0.0) is -inf.
     **
     **  expf failed: Argument out of domain
     **  Abort (core dumped)
     */

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

     main()
     {
     float ff;

        ff = logf(0.0f);  /* matherr() leaves error reporting to libmx.a */
        printf("logf(0.0f) is %f.\n\n",ff);
        fflush(stdout);

        ff = 0.0f;
        ff = 0.0f/ff;
        ff = expf(ff);  /* matherr() customized to force a core dump */
     }

     int matherr( register struct exception *x )
     {
     char *sterr;

        switch (x->type) {

        case DOMAIN:
           /* if it's a NaN, abort */

           if ( isnan(x->arg1) )
           {
                  sterr = strerror(EDOM);
                  fprintf(stderr, "%s failed: %s\n", x->name, sterr);
                  abort();
           }
           else if ( !strcmp(x->name, "sin") )
           {
                  x->retval = 0.0;   /* return 0.0 */
                  fprintf(stderr, "domain error in sin\n");
                 return (1); /* suppress libmx.a error message and setting of errno */
           }
           else
                  return (0); /* libmx.a prints error message and sets errno */
        break;

        default:
           return (0);    /* libmx.a prints error message and sets errno */
        }
     }

NOTES
     Errors detected by functions in libm.a can be detected by examining the
     return value and/or the global variable errno.  libmx.a is provided as an
     alternative error handling scheme for users who want to identify specific
     kinds of errors determined by libm functions without extensive
     modification of their source code.

     No extra overhead is incurred by programs using libmx.a if no error
     conditions are encountered in the libmx routines.