home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 1998 May / Pcwk5b98.iso / Borland / Cplus45 / BC45 / WILD32.PAK / MATHERR.C < prev    next >
C/C++ Source or Header  |  1995-08-29  |  5KB  |  134 lines

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