home *** CD-ROM | disk | FTP | other *** search
- Path: sparky!uunet!dtix!darwin.sura.net!mips!sdd.hp.com!cs.utexas.edu!hermes.chpc.utexas.edu!news.utdallas.edu!convex!dodson
- From: dodson@convex.COM (Dave Dodson)
- Newsgroups: comp.lang.fortran
- Subject: Re: Ignoring Least Significant Bit in a Real Compare
- Message-ID: <1992Aug21.212225.2961@news.eng.convex.com>
- Date: 21 Aug 92 21:22:25 GMT
- References: <1992Aug21.203055.29668@aero.org>
- Sender: usenet@news.eng.convex.com (news access account)
- Reply-To: dodson@convex.COM (Dave Dodson)
- Organization: Engineering, CONVEX Computer Corp., Richardson, Tx., USA
- Lines: 48
- Originator: dodson@bach.convex.com
- Nntp-Posting-Host: bach.convex.com
- X-Disclaimer: This message was written by a user at CONVEX Computer
- Corp. The opinions expressed are those of the user and
- not necessarily those of CONVEX.
-
- In article <1992Aug21.203055.29668@aero.org> wae@aero.org (William A. Emanuelsen) writes:
- >I am working on a 60-bit CDC machine in fortran-4. (stop laughing!) I
- >am trying to compare two real numbers with .EQ. in an 'if' statement.
- >But what happens is that the numbers I am comparing, and which should be
- >equal, fail the test since the 60th bit sometimes gets rounded in the
- >opposite direction.
- >
- >So what I did was to write a logical function, REQ, to replace the .EQ.
- >comparison:
- >
- > LOGICAL FUNCTION REQ(X1,X2)
- >C
- > REQ = .FALSE.
- > IF (ABS(X1-X2) .LT. EPS) REQ = .TRUE.
- > RETURN
- > END
- >
- >Where EPS is the smallest difference I expect to see between two numbers
- >that actually are unequal.
- >
- >So where I formerly had: IF (TEMP1 .EQ. TEMP2) ...
- >I now have: IF (REQ(TEMP1,TEMP2)) ...
- >
- >Is there a better (faster and more straightforward) way to ignore the
- >least significant bit when doing real compares in fortran-4?
- >(Bit-shifting seems to add more steps.)
-
- I would do something such as the following in hopes of eliminating EPS.
-
- LOGICAL FUNCTION REQ(X1,X2)
- C
- T = X1 + 0.5 * (X2-X1)
- REQ = X1 .EQ. T .OR. X2 .EQ. T
- RETURN
- END
-
- The idea is that if X1 and X2 are adjacent numbers, then the midpoint will
- have to be equal to one of the numbers. If there are enough numbers between
- X1 and X2, X3 will not equal either of X1 and X2. Only some testing will
- show what happens when X1 and X2 have only one or two numbers between them.
-
- I doubt if this code is faster than yours, but it may work better over a
- range of values of X1 and X2.
-
- ----------------------------------------------------------------------
-
- Dave Dodson dodson@convex.COM
- Convex Computer Corporation Richardson, Texas (214) 497-4234
-