From: boost-users-bounces@lists.boost.org [mailto:boost-users-bounces@lists.boost.org] On Behalf Of Brian O'Kennedy
Sent: Wednesday, April 07, 2010 3:16 PM
To: Boost-users@lists.boost.org
Subject: [Boost-users] [Boost.Test] Floating point NAN comparison

 

Hi, 

 

This bit of Boost.Test code fails:

 

//////////////////////////////////////////////////////////////////////

BOOST_AUTO_TEST_CASE( nantest )

{

  float a,b;

  a = b = 1.0;

  BOOST_CHECK_EQUAL( a, b );  // passes

  a = b = NAN;

  BOOST_CHECK_EQUAL( a, b );  // fails

}

//////////////////////////////////////////////////////////////////////

 

The failure is because, by definition, NAN != NAN. What I'm after is a test macro which takes this into account and will pass the equality test when both elements are NAN. 

 

BOOST_CHECK_CLOSE has the same behaviour.  Is the best solution to write my own custom predicate? 

 

You can use the TR1 bool isnan(a) function in the boost.math to check for NaNness.

 

\boost-sandbox\math_toolkit\libs\math\example\fpclassify.cpp gives some examples of

 

using boost::math::isnan;

 

\boost_1_42_0\libs\math\test\test_classify.cpp(68): BOOST_CHECK_EQUAL((::boost::math::isnan)(t), false);

 

gives some examples of using boost.test with NaNs

 

See the boost_pdf_1_41_0/math.pdf for details.

 

HTH

 

Paul

 

 

---

Paul A. Bristow

Prizet Farmhouse

Kendal, UK   LA8 8AB

+44 1539 561830, mobile +44 7714330204

pbristow@hetp.u-net.com