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