home *** CD-ROM | disk | FTP | other *** search
/ DOS/V Power Report 1997 May / VPR9705A.ISO / VPR_DATA / PROGRAM / CBTRIAL / SETUP / DATA.Z / MATHERRL.C < prev    next >
C/C++ Source or Header  |  1997-02-14  |  5KB  |  129 lines

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