home *** CD-ROM | disk | FTP | other *** search
/ Turbo Toolbox / Turbo_Toolbox.iso / tc20 / matherr.c < prev    next >
Text File  |  1988-08-28  |  5KB  |  144 lines

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