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

  1. Path: sparky!uunet!haven.umd.edu!darwin.sura.net!mips!think.com!Think.COM!sandee
  2. From: sandee@Think.COM (Daan Sandee)
  3. Newsgroups: comp.lang.fortran
  4. Subject: Re: Ignoring Least Significant Bit in a Real Compare
  5. Date: 21 Aug 1992 21:12:34 GMT
  6. Organization: TMC
  7. Lines: 44
  8. Distribution: world
  9. Message-ID: <173m82INNkmc@early-bird.think.com>
  10. References: <1992Aug21.203055.29668@aero.org>
  11. NNTP-Posting-Host: django.think.com
  12.  
  13. In article <1992Aug21.203055.29668@aero.org>, wae@aero.org (William A. Emanuelsen) writes:
  14. |> 
  15. |> TEMP1 = 17266436725301415722
  16. |> TEMP2 = 17266436725301415723
  17. |> 
  18. |> They should be equal since TEMP1 is assigned a number,
  19. |> and TEMP2 = TEMP3 + DELTA, where DELTA = TEMP1 - TEMP3.
  20. |> (It's really a bit more complicated than this.)
  21.  
  22.  This is (as you know) one of the facts of life when using floating point
  23.  arithmetic. The hardware does not guarantee that X+Y-Y is the same as X.
  24.  So my first reaction is, of course : you're SOL.
  25.  
  26. |> So what I did was to write a logical function, REQ, to replace the .EQ. 
  27. |> comparison:
  28. |> 
  29. |>       LOGICAL FUNCTION REQ(X1,X2)
  30. |>       REQ = .FALSE.
  31. |>       IF (ABS(X1-X2) .LT. EPS) REQ = .TRUE.
  32. |>       RETURN
  33. |>       END
  34. |> 
  35. |> So where I formerly had: IF (TEMP1 .EQ. TEMP2) ...
  36. |> I now have:              IF (REQ(TEMP1,TEMP2)) ...
  37.  
  38. It would be a lot faster if you had a statement function
  39.  
  40.         LOGICAL REQ
  41.         REQ(X1,X2) = ABS(X1-X2) .LT. EPS
  42.  
  43.  Not having an external function call saves two memory writes, an RJ,
  44.  three memory reads, and two EQ (jump) instructions, plus the cost of
  45.  not being able to optimize across a function call.
  46.  
  47.  Depending on how you use it, it might be faster to have a real function,
  48.  
  49.        REQ(X1,X2) = ABS(X1-X2) - EPS
  50.  
  51.  and then test with IF (REQ(X1,X2) .LT. 0.). It would save the compiler
  52.  the trouble of converting the result of the subtract into a logical value.
  53.  
  54. Daan Sandee                                           sandee@think.com
  55. Thinking Machines Corporation
  56. Cambridge, Mass 02142                                 (617) 234-5044
  57.