Boost logo

Boost Users :

Subject: Re: [Boost-users] [Boost.Test] Floating point NAN comparison
From: Paul A. Bristow (pbristow_at_[hidden])
Date: 2010-04-07 12:26:25


 

 

From: boost-users-bounces_at_[hidden] [mailto:boost-users-bounces_at_[hidden]] On Behalf Of Brian O'Kennedy
Sent: Wednesday, April 07, 2010 3:16 PM
To: Boost-users_at_[hidden]
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_at_[hidden]
 
 
 
 
 



Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net