Re: comparing doubles for equality



CBFalconer wrote:
Thad Smith wrote:
John Smith wrote:

This code for the comparison of fp types is taken from the C FAQ.
Any problems using it in a macro?

/* compare 2 doubles for equality */
#define DBL_ISEQUAL(a,b) (fabs((a)-(b))<=(DBL_EPSILON)*fabs((a)))
This construction is misleading and I would never use it, because
the implied function, determining whether two doubles are equal,
is not an accurate description of the returned value.

How about:

#define DUNEQUAL(a, b) (fabs((a)-(b)) > (DBL_EPSILON)*fabs((a)))

with a caveat against passing an a with side effects.

That misses the point. The only true equality test is given by a==b. If you need an epsilon, fine, but you should make it explicit. DBL_EPSILON isn't necessarily the correct choice for a particular application. In fact, the two tests above may not provide any advantage
over a strict equality.

--
Thad
.