forums.silverfrost.com
Welcome to the Silverfrost forums

Author Message
eric_carwardine

Joined: 13 Jun 2009
Posts: 63
Location: Perth, Western Australia

Posted: Fri Apr 17, 2015 2:04 am    Post subject: Comparing real quantities for 'equality'

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:
 Code: (1.0 - d) .lt. 1.0

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

Eric
JohnCampbell

Joined: 16 Feb 2006
Posts: 2159
Location: Sydney

 Posted: Fri Apr 17, 2015 3:28 am    Post subject: 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()*x*safety_factor ) would be an estimate of error or error = max ( epsilon()*x*safety_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
eric_carwardine

Joined: 13 Jun 2009
Posts: 63
Location: Perth, Western Australia

Posted: Fri Apr 17, 2015 4:11 am    Post subject: Re:

 JohnCampbell wrote: 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:

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

Eric
 Display posts from previous: All Posts1 Day7 Days2 Weeks1 Month3 Months6 Months1 Year Oldest FirstNewest First
 All times are GMT + 1 Hour Page 1 of 1