home *** CD-ROM | disk | FTP | other *** search
/ The Devil's Doorknob BBS Capture (1996-2003) / devilsdoorknobbbscapture1996-2003.iso / Dloads / OTHERUTI / TCPP30-3.ZIP / EXAMPLES.ZIP / MATHERR.C < prev    next >
C/C++ Source or Header  |  1992-02-18  |  5KB  |  146 lines

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