home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #18 / NN_1992_18.iso / spool / comp / lang / fortran / 3104 < prev    next >
Encoding:
Internet Message Format  |  1992-08-21  |  1.8 KB

  1. Path: sparky!uunet!dtix!darwin.sura.net!mips!sdd.hp.com!usc!aero.org!wae
  2. From: wae@aero.org (William A. Emanuelsen)
  3. Newsgroups: comp.lang.fortran
  4. Subject: Ignoring Least Significant Bit in a Real Compare
  5. Summary: How best to go about it?
  6. Message-ID: <1992Aug21.203055.29668@aero.org>
  7. Date: 21 Aug 92 20:30:55 GMT
  8. Sender: news@aero.org
  9. Followup-To: comp.lang.fortran
  10. Organization: The Aerospace Corporation, El Segundo, CA
  11. Lines: 41
  12.  
  13. I am working on a 60-bit CDC machine in fortran-4.  (stop laughing!) I
  14. am trying to compare two real numbers with .EQ. in an 'if' statement.  
  15. But what happens is that the numbers I am comparing, and which should be 
  16. equal, fail the test since the 60th bit sometimes gets rounded in the 
  17. opposite direction.
  18.  
  19. For example, I have two variables, TEMP1 and TEMP2.  
  20.  
  21. Both print out unformatted as 104.9661485585.  
  22.  
  23. But in Octal representation,
  24.  
  25. TEMP1 = 17266436725301415722
  26. TEMP2 = 17266436725301415723
  27.  
  28. They should be equal since TEMP1 is assigned a number,
  29. and TEMP2 = TEMP3 + DELTA, where DELTA = TEMP1 - TEMP3.
  30. (It's really a bit more complicated than this.)
  31.  
  32. So what I did was to write a logical function, REQ, to replace the .EQ. 
  33. comparison:
  34.  
  35.       LOGICAL FUNCTION REQ(X1,X2)
  36. C
  37.       REQ = .FALSE.
  38.       IF (ABS(X1-X2) .LT. EPS) REQ = .TRUE.
  39.       RETURN
  40.       END
  41.  
  42. Where EPS is the smallest difference I expect to see between two numbers 
  43. that actually are unequal.
  44.  
  45. So where I formerly had: IF (TEMP1 .EQ. TEMP2) ...
  46. I now have:              IF (REQ(TEMP1,TEMP2)) ...
  47.  
  48. Is there a better (faster and more straightforward) way to ignore the 
  49. least significant bit when doing real compares in fortran-4?
  50. (Bit-shifting seems to add more steps.)
  51. -- 
  52. William A. Emanuelsen    "You'll never get famous by repeating what
  53. wae@aerospace.aero.org      other people say." -- Bartlett's Mom.
  54.