Login
`
Templates, Tools and Utilities
|
||
Icetips Article
Back to article list
Search Articles
Add Comment
Printer friendly
Direct link
Par2: Comparing REALs 2002-04-24 -- Randy Goodhew What I use for REAL comparisons:
Returns:
-1 = a is less than b
1 = a is greater than b
0 = a is equal to b
! RealCompare(REAL a,REAL b,REAL order=0.0001),LONG
RealCompare PROCEDURE(REAL a,REAL b,REAL order)
e:LessThan EQUATE(-1)
e:Equal EQUATE(0)
e:GreaterThan EQUATE(1)
Result LONG,AUTO
CODE
a = ROUND(a,order)
b = ROUND(b,order)
IF a < b ; Result = e:LessThan
ELSIF a > b ; Result = e:GreaterThan
ELSIF a = b ; Result = e:Equal
END ! if
RETURN Result
John Davidson notes:
The "scientific" way of cpmparing reals is to check whether they differ by
less than a small value: ie
IF ABS( A - B ) > 0.0001 THEN ! they are unequal
ELSE ! they are equal
The small value is chosen to be significantly smaller than the typical
values being compared.
Richard Taylor adds:
If you're going to ROUND anyway, why not just change to DECIMAL instead and
eliminate all the problems. That way the rounding is automatic, math is done
as BCD, and equivalence comaprisons work correctly.
Today is November 21, 2024, 7:41 am This article has been viewed 35213 times.
|
|