home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c083 / 12.ddi / WILD32.PAK / MATHERR.C < prev    next >
Encoding:
C/C++ Source or Header  |  1993-12-02  |  5.1 KB  |  128 lines

  1. /*------------------------------------------------------------------------
  2.  * filename - matherr.c
  3.  *
  4.  * function(s)
  5.  *        _matherr - user-modifiable math error handler
  6.  *-----------------------------------------------------------------------*/
  7.  
  8. /* $Copyright: 1987$ */
  9.  
  10. #include <math.h>
  11.  
  12. #ifdef  UNIX_matherr
  13. #include <stdio.h>
  14. #include <process.h>
  15. #include <_io.h>
  16. #include <_math.h>
  17.  
  18. /*------------------------------------------------------------------------*
  19.  
  20. Name            _matherr - user-modifiable math error handler
  21.  
  22. Usage           #include <math.h>
  23.                 int _matherr(struct exception *e);
  24.  
  25. Prototype in    math.h
  26.  
  27. Description     When  exceptions are  detected in  the math  library then a
  28.                 call is made  to  __matherr()  with all the available
  29.                 information.
  30.  
  31.                 That function does very little, except to map the exception
  32.                 "why"  into either  ERANGE or  EDOMAIN in  errno. Its  main
  33.                 purpose is  to act as  a focal point  for changes in  error
  34.                 handling.
  35.  
  36.                 For example,  if you were  writing a spreadsheet  you might
  37.                 replace  this function with one which pops up an error
  38.                 window explaining something like:
  39.  
  40.                         "log (-2.0) caused domain error, in cell J7"
  41.  
  42.                 and then longjmp() to a  reset state in the spreadsheet and
  43.                 await the next command from the user.
  44.  
  45.                 The default version  of the _matherr routine masks
  46.                 underflow and precision errors; others errors are considered
  47.                 fatal.  It serves as a hook that you can replace when
  48.                 writing your own math error handling routine.
  49.  
  50.                 The rationale for masking underflow and precision errors
  51.                 is that these are not errors according to the ANSI C spec.
  52.                 Consequently, you will get
  53.                         exp(-1000) = 0
  54.                         sin(1e100) = NAN
  55.                 without any error or warning, even though there is a total
  56.                 loss of precision in both cases.  You can trap these errors
  57.                 by modifying _matherr.
  58.  
  59.                 The possible errors are
  60.                         DOMAIN, SING, OVERFLOW, UNDERFLOW, TLOSS, PLOSS
  61.                 and listed in <math.h>.  As explained above, UNDERFLOW and
  62.                 TLOSS are masked by the default _matherr.  PLOSS is not
  63.                 supported by TC and is not generated by any library functions.
  64.                 The remaining errors, DOMAIN, SING, and OVERFLOW, are fatal
  65.                 with the default _matherr.
  66.  
  67.                 You  can  modify  _matherr  to  be  a  custom error handling
  68.                 routine (such as one that catches and resolves certain type
  69.                 of  errors); the  modified _matherr  should return  0 if  it
  70.                 failed to resolve  the error, or non-zero if  the error was
  71.                 resolved. When _matherr returns non-zero, no  error message
  72.                 is printed, and errno is not changed.
  73.  
  74.                 The  important thing  is  that  we  don't  know what error
  75.                 handling you want, but you are assured that all errors will
  76.                 arrive at  _matherr() with all  the information you  need to
  77.                 design a custom format.
  78.  
  79.                 We  do not  ship as  standard the  function named _matherr()
  80.                 which may be  familiar to UNIX users, since  the ANSI x3j11
  81.                 draft specifies  an incompatible style. This  version is as
  82.                 close as we could get  without breaking the ANSI rules. You
  83.                 can, however, convert this version to the UNIX style if you
  84.                 prefer. The necessary code is included but switched off.
  85.  
  86. Return value    The default return  value for _matherr is simply  0.
  87.                 _matherr can also modify  e->retval, which propagates through
  88.                 __matherr back to the original caller.
  89.  
  90.                 When _matherr returns 0, (indicating that it was not able to
  91.                 resolve the error) __matherr sets  errno and prints an error
  92.                 message.
  93.  
  94.                 When _matherr returns non-zero, (indicating that it was able
  95.                 to resolve the error) errno is not set and no messages are
  96.                 printed.
  97.  
  98. *-------------------------------------------------------------------------*/
  99.  
  100. int _RTLENTRY _matherr (struct exception *e)
  101. {
  102.     char errMsg[ 80 ];
  103.     sprintf (errMsg,
  104.         "%s (%8g,%8g): %s\n", e->name, e->arg1, e->arg2, _mathwhy [e->type - 1]);
  105.     _ErrorExit(errMsg);
  106. }
  107.  
  108. #else   /* ! UNIX_matherr */
  109.  
  110. int _RTLENTRY _matherr(struct exception *e)
  111. {
  112.         if (e->type == UNDERFLOW)
  113.         {
  114.                 /* flush underflow to 0 */
  115.                 e->retval = 0;
  116.                 return 1;
  117.         }
  118.         if (e->type == TLOSS)
  119.         {
  120.                 /* total loss of precision, but ignore the problem */
  121.                 return 1;
  122.         }
  123.         /* all other errors are fatal */
  124.         return 0;
  125. }
  126.  
  127. #endif
  128.