home *** CD-ROM | disk | FTP | other *** search
- Path: sparky!uunet!dtix!darwin.sura.net!mips!sdd.hp.com!usc!aero.org!wae
- From: wae@aero.org (William A. Emanuelsen)
- Newsgroups: comp.lang.fortran
- Subject: Ignoring Least Significant Bit in a Real Compare
- Summary: How best to go about it?
- Message-ID: <1992Aug21.203055.29668@aero.org>
- Date: 21 Aug 92 20:30:55 GMT
- Sender: news@aero.org
- Followup-To: comp.lang.fortran
- Organization: The Aerospace Corporation, El Segundo, CA
- Lines: 41
-
- 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.
-
- For example, I have two variables, TEMP1 and TEMP2.
-
- Both print out unformatted as 104.9661485585.
-
- But in Octal representation,
-
- TEMP1 = 17266436725301415722
- TEMP2 = 17266436725301415723
-
- They should be equal since TEMP1 is assigned a number,
- and TEMP2 = TEMP3 + DELTA, where DELTA = TEMP1 - TEMP3.
- (It's really a bit more complicated than this.)
-
- 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.)
- --
- William A. Emanuelsen "You'll never get famous by repeating what
- wae@aerospace.aero.org other people say." -- Bartlett's Mom.
-