home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c082_122 / 2.ddi / MATHSRC.ZIP / _MATHERR.C < prev    next >
Encoding:
C/C++ Source or Header  |  1992-06-10  |  4.2 KB  |  123 lines

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