0

I'm using the Boost unit test-framework to compare doubles with each other. The results are as expected, but BOOST_CHECK_CLOSE does not recognize them correctly I guess. The code is essentially as follows:

BOOST_AUTO_TEST_CASE(FooBarTest)
{
    double foo = 2.2500000047015632e-006;
    double bar = 0.0;
    double tolerance = 90.0;
    BOOST_CHECK_CLOSE(foo, bar, tolerance);
}

This fails with the following message:

error in [...]: difference{1.#INF%} between foo{2.2500000047015632e-006} and bar{0} exceeds 90%

I'm using Boost 1.55 with VC100 (Visual Studio 10 compiler). I'm compiling Win32 Release.

Is this a result I should expect? I would imagine that these values are close enough to each other and it should return success. Is Boost internally dividing by 0.0?

MOnsDaR
  • 8,401
  • 8
  • 49
  • 70

2 Answers2

3

You can use BOOST_CHECK_SMALL to check closeness to zero. For more background information, see: Boost UTF documentation, article on floating-point comparison algorithms.

Paul Omta
  • 1,703
  • 1
  • 12
  • 17
1

That's because bar is zero. BOOST_CHECK_CLOSE checks if the two values are 'close', that is, in your case, with 90% of each other. For that, you use division, and you can't divide by 0.

Or, as this this answer so succinctly explains - zero is not close to anything.

Community
  • 1
  • 1
zmbq
  • 38,013
  • 14
  • 101
  • 171