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

  1. /*------------------------------------------------------------------------
  2.  * filename - matherrl.c
  3.  *
  4.  * function(s)
  5.  *        _matherrl - user-modifiable long double math error handler
  6.  *-----------------------------------------------------------------------*/
  7.  
  8. /* $Copyright: 1987$ */
  9.  
  10. #include <math.h>
  11.  
  12. #ifdef  UNIX__matherrl
  13. #include <stdio.h>
  14. #include <process.h>
  15. #include <_io.h>
  16. #include <_math.h>
  17.  
  18. /*------------------------------------------------------------------------*
  19.  
  20. Name            _matherrl - user-modifiable long double math error handler
  21.  
  22. Usage           #include <math.h>
  23.                 int _matherrl(struct _exceptionl *e);
  24.  
  25. Prototype in    math.h
  26.  
  27. Description     When  exceptions are  detected in  the long double math
  28.                 library then a call is made to __matherrl() with all of
  29.                 the available 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.                         "logl (-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 Turbo C's _matherrl 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.                         expl(-1000) = 0
  54.                         sinl(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 _matherrl.
  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 _matherrl.  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 _matherrl.
  66.  
  67.                 You  can  modify  _matherrl  to  be  a  custom error handling
  68.                 routine (such as one that catches and resolves certain type
  69.                 of  errors); the  modified _matherrl  should return  0 if  it
  70.                 failed to resolve  the error, or non-zero if  the error was
  71.                 resolved. When _matherrl 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  _matherrl() with all  the information you  need to
  77.                 design a custom format.
  78.  
  79. Return value    The default return  value for _matherrl is simply  0.
  80.                 _matherrl can also modify  e->retval, which propagates through
  81.                 __matherrl back to the original caller.
  82.  
  83.                 When _matherrl returns 0, (indicating that it was not able to
  84.                 resolve the error) __matherrl sets  errno and prints an error
  85.                 message.
  86.  
  87.                 When _matherrl returns non-zero, (indicating that it was able
  88.                 to resolve the error) errno is not set and no messages are
  89.                 printed.
  90.  
  91. *-------------------------------------------------------------------------*/
  92.  
  93. int _RTLENTRY _matherrl (struct _exceptionl *e)
  94. {
  95.     char errMsg[ 80 ];
  96.     sprintf (errMsg,
  97.         "%s (%8Lg,%8Lg): %s\n", e->name, e->arg1, e->arg2, whyS [e->type - 1]);
  98.     _ErrorExit(errMsg);
  99. }
  100.  
  101. #else   /* ! UNIX_matherr */
  102.  
  103. int _RTLENTRY _matherrl(struct _exceptionl *e)
  104. {
  105.         if (e->type == UNDERFLOW)
  106.         {
  107.                 /* flush underflow to 0 */
  108.                 e->retval = 0;
  109.                 return 1;
  110.         }
  111.         if (e->type == TLOSS)
  112.         {
  113.                 /* total loss of precision, but ignore the problem */
  114.                 return 1;
  115.         }
  116.         /* all other errors are fatal */
  117.         return 0;
  118. }
  119.  
  120.  
  121. #endif
  122.