home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #26 / NN_1992_26.iso / spool / comp / lang / fortran / 4227 < prev    next >
Encoding:
Text File  |  1992-11-07  |  2.5 KB  |  91 lines

  1. Xref: sparky comp.lang.fortran:4227 comp.lang.c:16089
  2. Path: sparky!uunet!destroyer!cs.ubc.ca!unixg.ubc.ca!sitka.triumf.ca!morgan
  3. From: morgan@sitka.triumf.ca (Morgan Burke)
  4. Newsgroups: comp.lang.fortran,comp.lang.c
  5. Subject: SGI floating point exceptions
  6. Date: 6 Nov 1992 18:40:06 GMT
  7. Organization: TRIUMF, Vancouver BC
  8. Lines: 78
  9. Distribution: world
  10. Message-ID: <1dee66INNe4a@iskut.ucs.ubc.ca>
  11. NNTP-Posting-Host: sitka.triumf.ca
  12.  
  13. I'm having difficulty creating a floating point exception handler
  14. for an application running on SGI's Irix 4.0 (System V-based),
  15. with f77 3.4.
  16.  
  17. According to the man pages, the routine "handle_sigfpes" is used
  18. to manage floating point exceptions (the normal signal processing
  19. facilities seem to have no effect).  However, I get very strange
  20. results using this routine from both Fortran and C.
  21.  
  22. First, the Fortran case:
  23.  
  24.       PROGRAM sig
  25. #include <fsigfpe.h>
  26.       REAL*4 a/1.0/,b/0.0/
  27.       CALL handle_sigfpes(FPE_ON,FPE_EN_DIVZERO,0,FPE_ABORT_ON_ERROR,0)
  28.       WRITE(6,*) a/b
  29.       WRITE(6,*) b**(-2)
  30.       WRITE(6,*) LOG(b)
  31.       END
  32.  
  33. % f77 sig.f -lfpe
  34. % a.out
  35.    3.4028235E+38
  36.  Infinity
  37.    3.4028235E+38
  38.  Infinity
  39.    3.4028235E+38
  40.  Infinity
  41.    3.4028235E+38
  42. ....and so on, in an infinite loop
  43.  
  44. The normal output (ie. without the call to handle_sigfpes) is:
  45.  Infinity
  46.  Infinity
  47.  Infinity
  48.  
  49. Now, the C case:
  50.  
  51. #include <sigfpe.h>
  52. #include <math.h>
  53. main()
  54. {
  55.   float a=1.0,b=0.0;
  56.   handle_sigfpes(_ON, _EN_DIVZERO, 0, _ABORT_ON_ERROR, 0 );
  57.   printf("%e\n",a/b);
  58.   printf("%e\n",pow(b,-2));
  59.   printf("%e\n",log(b));
  60. }
  61.  
  62. % cc sig.c -lfpe -lm
  63. % a.out
  64. 3.402823e+38
  65. 1.797693e+308
  66. sigfpe_catch: **ABORT(internal) - failure to analyze exception conditions.
  67.         pc = 0x403a68, fcsr = 0x800420
  68. Floating exception (core dumped)
  69.  
  70. Removing the call to handle_sigfpes, I get:
  71. Infinity
  72. Infinity
  73. -Infinity
  74.  
  75. Questions:
  76. ==========
  77.  
  78. 1.  Why does the handler trap log(0) in C, but not an explicit
  79.     divide-by-zero, when that is what I have asked for?
  80. 2.  Why do results that are normally "Infinity" come back as legal
  81.     floating point values (the maximal values, I assume) when the
  82.     handler is used?
  83. 3.  Any idea why the Fortran routine goes berserk?
  84. 4.  Why does LOG(0) return +Infinity in f77?
  85. 5.  I want the application to core dump (like it did for log(0) in C)
  86.     for all of these examples.  Anybody know a better way to do this?
  87. 6.  Who do I hunt down and kill for stupidities (IMHO) like "NaN" and
  88.     "Infinity" in the IEEE floating point standard?
  89.  
  90. -- Morgan Burke (morgan@reg.triumf.ca)
  91.