View previous topic :: View next topic |
Author |
Message |
eric_carwardine
Joined: 13 Jun 2009 Posts: 70 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:
Which suggests a supplementary question of getting the machine constant 'd'
Eric |
|
Back to top |
|
|
JohnCampbell
Joined: 16 Feb 2006 Posts: 2554 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 |
|
Back to top |
|
|
eric_carwardine
Joined: 13 Jun 2009 Posts: 70 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 |
|
Back to top |
|
|
|