home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c065 / 2.ddi / MATH.ZIP / _MATHERR.C next >
Encoding:
C/C++ Source or Header  |  1990-06-07  |  4.3 KB  |  148 lines

  1. /*------------------------------------------------------------------------
  2.  * filename - _matherr.c
  3.  *
  4.  * function(s)
  5.  *        _matherr - floating-point error handling routine
  6.  *-----------------------------------------------------------------------*/
  7.  
  8. /*[]------------------------------------------------------------[]*/
  9. /*|                                                              |*/
  10. /*|     Turbo C Run Time Library - Version 3.0                   |*/
  11. /*|                                                              |*/
  12. /*|                                                              |*/
  13. /*|     Copyright (c) 1987, 1990 by Borland International        |*/
  14. /*|     All Rights Reserved.                                     |*/
  15. /*|                                                              |*/
  16. /*[]------------------------------------------------------------[]*/
  17.  
  18.  
  19. #include <_math.h>
  20. #include <math.h>
  21. #include <stdio.h>
  22. #include <errno.h>
  23.  
  24.  
  25. /*-------------------------------------------------------------------------*
  26.  
  27. Name        _matherr - floating-point error handling routine
  28.  
  29. Usage        #include<math.h>
  30.         double     _matherr (_mexcep why,
  31.                    char    *fun,
  32.                    double  *arg1p,
  33.                    double  *arg2p,
  34.                    double  retval);
  35.  
  36.  
  37. Prototype in    math.h
  38.  
  39. Description    _matherr serves as a focal  point for error handling in all
  40.         math library functions; it  calls matherr and processes the
  41.         return value from matherr.  _matherr should never be called
  42.         directly by user programs.
  43.  
  44.         Whenever  an  error  occurs  in  one  of  the  math library
  45.         routines _matherr is called with several arguments.
  46.  
  47.         _matherr does four things :
  48.             . It  uses its arguments  to fill out  an exception
  49.               structure.
  50.  
  51.             . It  calls   matherr  with  e,  a  pointer  to the
  52.               exception  structure,  to   see  if  matherr    can
  53.               resolve the error.
  54.  
  55.             . It  examines    the  return  value from  matherr as
  56.               follows:
  57.                 If matherr  returns 0, (indicating  that it
  58.                 was not able to resolve the error) _matherr
  59.                 sets errno and prints an error message
  60.  
  61.                 If  matherr  returns  non-zero, (indicating
  62.                 that  it  was  able  to  resolve the error)
  63.                 _matherr is  silent; it does not  set errno
  64.                 or print any messages.
  65.  
  66.             . It returns e->retval to the original caller. Note
  67.               that    matherr might  modify e->retval  to specify
  68.               the  value  it  wants   propagated  back  to    the
  69.               original caller.
  70.  
  71.         When  _matherr    sets  errno  (based  on  a  0  return  from
  72.         matherr), it maps the kind of error that occurred (from the
  73.         type field in the exception  structure) onto an errno value
  74.         of either EDOM or ERANGE.
  75.  
  76. Return value    _matherr returns the value, e->retval.
  77.  
  78. *-------------------------------------------------------------------------*/
  79. #ifdef    UNIX_matherr
  80. char *whyS [] =
  81.     {
  82.     "argument domain error",
  83.     "argument singularity ",
  84.     "overflow range error ",
  85.     "underflow range error",
  86.     "total loss of significance",
  87.     "partial loss of significance"
  88.     };
  89.  
  90. double _matherr(_mexcep why,
  91.         char    *fun,
  92.         double    *arg1p,
  93.         double    *arg2p,
  94.         double    retval)
  95. {
  96.     struct    exception    e;
  97.  
  98.     e.type = why;
  99.     e.name = fun;
  100.     e.arg1 = (NULL == arg1p) ? 0 : *arg1p;
  101.     e.arg2 = (NULL == arg2p) ? 0 : *arg2p;
  102.     e.retval = retval;
  103.  
  104.     if (matherr (& e) == 0)
  105.         {
  106.         fprintf (stderr, "%s (%8g,%8g): %s\n", fun,
  107.                                *arg1p,
  108.                                *arg2p,
  109.                                whyS [why - 1]);
  110.  
  111.         errno = ((OVERFLOW == why) || (UNDERFLOW == why)) ? ERANGE : EDOM;
  112.         }
  113.     return    e.retval;
  114. }
  115. #else
  116. char *whyS [] =
  117.     {
  118.     "DOMAIN",       /* argument domain error -- log (-1)           */
  119.     "SING",           /* argument singularity    -- pow (0,-2))       */
  120.     "OVERFLOW",       /* overflow range error    -- exp (1000)       */
  121.     "UNDERFLOW",   /* underflow range error -- exp (-1000)       */
  122.     "TLOSS",       /* total loss of significance -- sin(10e70) */
  123.     "PLOSS"           /* partial loss of signif. -- not used       */
  124.     };
  125.  
  126. double    _matherr(_mexcep why,
  127.          char     *fun,
  128.          double  *arg1p,
  129.          double  *arg2p,
  130.          double  retval)
  131. {
  132.     struct    exception    e;
  133.  
  134.     e.type = why;
  135.     e.name = fun;
  136.     e.arg1 = (NULL == arg1p) ? 0 : *arg1p;
  137.     e.arg2 = (NULL == arg2p) ? 0 : *arg2p;
  138.     e.retval = retval;
  139.  
  140.     if (matherr (& e) == 0)
  141.         {
  142.         fprintf (stderr, "%s: %s error\n", fun, whyS [why - 1]);
  143.         errno = ((OVERFLOW == why) || (UNDERFLOW == why)) ? ERANGE : EDOM;
  144.         }
  145.     return    e.retval;
  146. }
  147. #endif
  148.