home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / World_Of_Computer_Software-02-387-Vol-3of3.iso / m / mawk11as.zip / MATHERR.C < prev    next >
C/C++ Source or Header  |  1992-01-22  |  3KB  |  177 lines

  1.  
  2. /********************************************
  3. matherr.c
  4. copyright 1991, Michael D. Brennan
  5.  
  6. This is a source file for mawk, an implementation of
  7. the AWK programming language.
  8.  
  9. Mawk is distributed without warranty under the terms of
  10. the GNU General Public License, version 2, 1991.
  11. ********************************************/
  12.  
  13. /*$Log:    matherr.c,v $
  14.  * Revision 5.1  91/12/05  07:56:18  brennan
  15.  * 1.1 pre-release
  16.  * 
  17. */
  18.  
  19. #include  "mawk.h"
  20. #include  <math.h>
  21.  
  22. #if   FPE_TRAPS_ON
  23. #include <signal.h>
  24.  
  25. /* machine dependent changes might be needed here */
  26.  
  27. static void  fpe_catch( signal, why)
  28.   int signal, why ;
  29. {
  30.  
  31. #if   NOINFO_SIGFPE
  32.  /* some systems give no hook to find out what the exception
  33.     was -- stuff like this is why people still use fortran 
  34.  
  35.     If this fits, #define NOINFO_SIGFPE 1 in  your config.h
  36. */
  37.   rt_error("floating point exception, probably overflow") ;
  38. #else
  39.  
  40.   switch(why)
  41.   {
  42.     case FPE_ZERODIVIDE :
  43.        rt_error("division by zero") ;
  44.  
  45.     case FPE_OVERFLOW  :
  46.        rt_error("floating point overflow") ;
  47.  
  48.     default :
  49.       rt_error("floating point exception") ;
  50.   }
  51. #endif  
  52. }
  53.  
  54. void   fpe_init()
  55. { (void) signal(SIGFPE, fpe_catch) ; }
  56.  
  57. #else  /* FPE_TRAPS_ON==0 */
  58.  
  59. void  fpe_init()
  60. {
  61.   TURN_OFF_FPE_TRAPS() ;
  62. }
  63. #endif
  64.  
  65. #if  HAVE_MATHERR
  66.  
  67. #if  ! FPE_TRAPS_ON
  68.  
  69. /* If we are not trapping math errors, we will shutup the library calls
  70. */
  71.  
  72. int  matherr( e )
  73.   struct exception *e ;
  74. { return 1 ; } 
  75.  
  76. #else   /* print error message and exit */
  77.  
  78. int matherr( e )
  79.   struct exception  *e ;
  80. { char *error ;
  81.  
  82.   switch( e->type )
  83.   {
  84.     case  DOMAIN :
  85.     case  SING :
  86.             error = "domain error" ;
  87.             break ;
  88.  
  89.     case  OVERFLOW :
  90.             error = "overflow" ;
  91.             break ;
  92.  
  93.     case  TLOSS :
  94.     case  PLOSS :
  95.             error = "loss of significance" ;
  96.             break ;
  97.  
  98.     case  UNDERFLOW :
  99.             e->retval = 0.0 ;
  100.             return  1 ;  /* ignore it */
  101.   }
  102.  
  103.   if ( strcmp(e->name, "atan2") == 0 )
  104.       rt_error("atan2(%g,%g) : %s" ,
  105.          e->arg1, e->arg2, error ) ;
  106.   else
  107.       rt_error("%s(%g) : %s" , e->name, e->arg1, error) ;
  108.  
  109.   /* won't get here */
  110.   return 0 ;
  111. }
  112. #endif   /* FPE_TRAPS */
  113.  
  114. #endif   /*  HAVE_MATHERR */
  115.  
  116.  
  117. /* this is how one gets the libm calls to do the right
  118. thing on bsd43_vax
  119. */
  120.  
  121. #ifdef   BSD43_VAX
  122.  
  123. #include <errno.h>
  124.  
  125. double infnan( arg )
  126.   int arg ;
  127. {
  128.   switch(arg)
  129.   {
  130.     case  ERANGE : errno = ERANGE ; return HUGE ;
  131.     case -ERANGE : errno = EDOM ; return -HUGE ;
  132.     default :  errno = EDOM ; 
  133.   }
  134.   return 0.0 ;
  135. }
  136.  
  137. #endif  /* BSD43_VAX */
  138.  
  139. /* This routine is for XENIX-68K 2.3A.
  140.     Error check routine to be called after fp arithmetic.
  141. */
  142.  
  143. #if SW_FP_CHECK
  144. /* Definitions of bit values in iserr() return value */
  145.  
  146. #define OVFLOW        2
  147. #define UFLOW        4
  148. #define ZERODIV        8
  149. #define OVFLFIX        32
  150. #define INFNAN        64
  151.  
  152. void
  153. fpcheck()
  154. {
  155.     register int fperrval ;
  156.     char *errdesc ;
  157.  
  158.     if ((fperrval = iserr()) == 0)
  159.         return ;    /* no error */
  160.  
  161.     errdesc = (char *) 0 ;
  162.  
  163.     if (fperrval & INFNAN)
  164.         errdesc = "arg is infinity or NAN" ;
  165.     else if (fperrval & ZERODIV)
  166.         errdesc = "division by zero" ;
  167.     else if (fperrval & OVFLOW)
  168.         errdesc = "overflow" ;
  169.     else if (fperrval & UFLOW)
  170.         ;        /* ignored */
  171.  
  172.     if (errdesc)
  173.         rt_error("%s", errdesc) ;
  174. }
  175.  
  176. #endif
  177.