Notifications
Clear all
[Closed] close_enough confusion
Apr 03, 2014 5:18 pm
a=18.0
b=20.0
close_enough a b 1000000
true
a=0.128178
b=0.015844
close_enough a b 1000000
false
Why? Isn’t the difference between values in the first case much greater than in the second?
3 Replies
Apr 03, 2014 5:18 pm
close_enough does a relative comparison of the difference which would be in your case boils down to the following:
(20.0 – 18.0)/20.0 = 0.1
(0.128178 – 0.015844)/0.128178 = 0.876391
btw: this not the exact math used by close_enough but it’s close enough to illustrate what’s going on.
if you want an absolute comparison then something like
fn near_enough f1 f2 tol = (abs (f1 - f2) <= tol;)
should do the trick.