5

I'm using the Boost::Test library, and I am trying to check if an actual percent value is close to the expected value:

BOOST_CHECK_CLOSE_FRACTION(
    items[i].ExpectedPercent,
    items[i].ActualCount / totalCount,
    0.05);

For some reason this check fails even when the values are close enough:

difference between items[i].ExpectedPercent{0.40000000000000002}
   and items[i].ActualCount / totalReturned{0.42999999999999999}
                                    exceeds 0.050000000000000003

Is this a problem with Boost or a problem with how I am using Boost?

iano
  • 2,061
  • 1
  • 18
  • 22

3 Answers3

4

After some testing, it turns out that the documentation for BOOST_CHECK_CLOSE_FRACTION is incorrect. The tolerance should be specified as a fraction of the expected value.

So, TFAE:

BOOST_CHECK(abs(x - y) < (min(x, y) * 0.1));
BOOST_CHECK_CLOSE(x, y, 10);
BOOST_CHECK_CLOSE_FRACTION(x, y, 0.1);
iano
  • 2,061
  • 1
  • 18
  • 22
  • 1
    I'd suggest you to check if there is a related report in Boost Trac and if not, report it there or send your comment to Boost mailing list. – mloskot Feb 19 '10 at 17:08
2

It is a problem with how you are using boost.

The last argument is a percent tolerance, not an absolute variance value. 5% of 0.4 is 0.02.

Omnifarious
  • 54,333
  • 19
  • 131
  • 194
1

Obviously BOOST_CHECK_CLOSE and BOOST_CHECK_CLOSE_FRACTION won't work if you check if something is near 0. Then you can use:

BOOST_CHECK(abs(x - y) < accurancy);
Janek Olszak
  • 4,067
  • 1
  • 28
  • 22