Silverfrost Forums

Welcome to our forums

Comparing real quantities for 'equality'

17 Apr 2015 1:04 #16218

G'day, folks 😄

The compiler tells me I'm using an unreliable method of comparing two real quantities for equality.

I believe there is a 'safe' method of comparison, based on the inequality:

(1.0 - d) .lt. 1.0

Which suggests a supplementary question of getting the machine constant 'd'

Eric

17 Apr 2015 2:28 #16219

Eric,

You should not get a warning with .lt. You would if you wrote 'if ( d .eq. 1.0 )', where the more 'robust' method is to write: if ( abs(d-1.0) < error ).

For an estimate of error, there are a few considerations:

  1. the intrinsic epsilon() gives an indication of the smallest possible value for error, although
  2. you want to choose a larger value if near enough is close enough.
  3. note that error varies with value, so something like: if ( abs(d-x) < epsilon()xsafety_factor ) would be an estimate of error or error = max ( epsilon()xsafety_factor, 1.e-10 )

Often it is easier to just use ' if ( d .eq. 1.0 ) ' and ignore the error, although if there is any round-off error in calculating d, the test could fail.

John

17 Apr 2015 3:11 #16220

Quoted from JohnCampbell Eric,

You should not get a warning with .lt.

Many thanks, John. That certainly is useful.

My mistake; it wasn't .lt. after all. The warning was actually generated from:

   0690         if( amod(uv,si) .eq. 0.0 ) return
   0691   C
WARNING - Comparing floating point quantities for equality may give misleading results

Eric

Please login to reply.