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

  1. Path: sparky!uunet!dtix!darwin.sura.net!mips!sdd.hp.com!cs.utexas.edu!hermes.chpc.utexas.edu!news.utdallas.edu!convex!dodson
  2. From: dodson@convex.COM (Dave Dodson)
  3. Newsgroups: comp.lang.fortran
  4. Subject: Re: Ignoring Least Significant Bit in a Real Compare
  5. Message-ID: <1992Aug21.212225.2961@news.eng.convex.com>
  6. Date: 21 Aug 92 21:22:25 GMT
  7. References: <1992Aug21.203055.29668@aero.org>
  8. Sender: usenet@news.eng.convex.com (news access account)
  9. Reply-To: dodson@convex.COM (Dave Dodson)
  10. Organization: Engineering, CONVEX Computer Corp., Richardson, Tx., USA
  11. Lines: 48
  12. Originator: dodson@bach.convex.com
  13. Nntp-Posting-Host: bach.convex.com
  14. X-Disclaimer: This message was written by a user at CONVEX Computer
  15.               Corp. The opinions expressed are those of the user and
  16.               not necessarily those of CONVEX.
  17.  
  18. In article <1992Aug21.203055.29668@aero.org> wae@aero.org (William A. Emanuelsen) writes:
  19. >I am working on a 60-bit CDC machine in fortran-4.  (stop laughing!) I
  20. >am trying to compare two real numbers with .EQ. in an 'if' statement.  
  21. >But what happens is that the numbers I am comparing, and which should be 
  22. >equal, fail the test since the 60th bit sometimes gets rounded in the 
  23. >opposite direction.
  24. >
  25. >So what I did was to write a logical function, REQ, to replace the .EQ. 
  26. >comparison:
  27. >
  28. >      LOGICAL FUNCTION REQ(X1,X2)
  29. >C
  30. >      REQ = .FALSE.
  31. >      IF (ABS(X1-X2) .LT. EPS) REQ = .TRUE.
  32. >      RETURN
  33. >      END
  34. >
  35. >Where EPS is the smallest difference I expect to see between two numbers 
  36. >that actually are unequal.
  37. >
  38. >So where I formerly had: IF (TEMP1 .EQ. TEMP2) ...
  39. >I now have:              IF (REQ(TEMP1,TEMP2)) ...
  40. >
  41. >Is there a better (faster and more straightforward) way to ignore the 
  42. >least significant bit when doing real compares in fortran-4?
  43. >(Bit-shifting seems to add more steps.)
  44.  
  45. I would do something such as the following in hopes of eliminating EPS.
  46.  
  47.       LOGICAL FUNCTION REQ(X1,X2)
  48. C
  49.       T = X1 + 0.5 * (X2-X1)
  50.       REQ = X1 .EQ. T .OR. X2 .EQ. T
  51.       RETURN
  52.       END
  53.  
  54. The idea is that if X1 and X2 are adjacent numbers, then the midpoint will
  55. have to be equal to one of the numbers.  If there are enough numbers between
  56. X1 and X2, X3 will not equal either of X1 and X2.  Only some testing will
  57. show what happens when X1 and X2 have only one or two numbers between them.
  58.  
  59. I doubt if this code is faster than yours, but it may work better over a
  60. range of values of X1 and X2.
  61.  
  62. ----------------------------------------------------------------------
  63.  
  64. Dave Dodson                                     dodson@convex.COM
  65. Convex Computer Corporation      Richardson, Texas      (214) 497-4234
  66.