Boost logo

Boost :

From: John Maddock (john_at_[hidden])
Date: 2007-05-15 12:09:14


Jody Hagins wrote:
> Here's a chance for someone to finally clear my understanding of how
> to compare floating point value... please ?!?!?

:-)

Let's say you want the tolerance to be N*eps where eps is the machine
epsilon and N is you level of tolerance, then you can use either:

BOOST_CHECK_CLOSE(a, b, N*100*numeric_limits<T>::epsilon()); // tolerance as
a persent

or:

BOOST_CHECK_FRACTION(a, b, N*numeric_limits<T>::epsilon()); // tolerance as
a fraction.

There are still a couple of things that can get you:

1) This will fail if one of a and b is zero and the other isn't. Generally
speeking you may want to treat all values below some threshold as
"effectively zero", but what that threshold is depends on your use case.

2) This fails if a and b are both infinity, probably a Boost.Test bug.

3) This fails on MacOS X with T=long double, this is a problem with the
excessively screwy long double on that platform: numeric_limits<long
double>::epsilon() is defined to be the same as
numeric_limits<double>::min(), which is very small indeed and much smaller
than the usual "sane" value for epsilon of ldexp(1.0, 1-numeric_limits<long
double>::digits). The latter expression is a viable replacement for if you
care about MacOS X BTW.

HTH, John.


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk