[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? Brian
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
On 7 April 2010 17:26, Paul A. Bristow
*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
Hi,
Thanks for the reply, but you misunderstood my question slightly. I can
check IF something is a NAN simply by doing:
BOOST_CHECK( std::isnan( a ) );
but what I really want is to cycle through two std::vector<float> vectors
and compare them. Doing so the normal way reports an inequality when
elements at the same index in both vectors are NAN. I want that check to
succeed, since as far as I'm concerned the vectors are equal if they contain
the same values.
I solved this by writing a custom predicate for Boost.Test:
/// ------------------ SNIP --------------------------------
#include
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
participants (2)
-
Brian O'Kennedy
-
Paul A. Bristow