matherr(3M)

matherr - error-handling function

As shipped in MIPSpro 7.4.3m.

NAME
     matherr - error-handling function

SYNOPSIS
     #include <math.h>

     int matherr (struct exception*x);

DESCRIPTION
     matherr is invoked by math functions in libmx.a when errors are
     detected.  To use matherr, libmx.a must 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 in the SYNOPSIS.  When an error
     occurs, a pointer to the exception structure x is 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 type argument 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 name argument points to a string containing the name of the
     function that incurred the error.  The arg1 and arg2 arguments 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 is
     printed by the calling function, and errno is not 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     --       --        --         M          --
 (arg = +/-inf)    M        --       --        --         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.
============================================================================

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.

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