Boost C++ Libraries: Ticket #13011: BOOST_TEST broken with floating point relational operators https://svn.boost.org/trac10/ticket/13011 <p> This is one of those crazy "Surely I must be doing something wrong here" things. But as far as I can tell, floating point comparisons don't work at all with the bare BOOST_TEST macro. A minimal reproduction </p> <hr /> <pre class="wiki">#define BOOST_AUTO_TEST_MAIN #include &lt;boost/test/auto_unit_test.hpp&gt; BOOST_AUTO_TEST_CASE(Foo) { BOOST_TEST(0.0 &lt; 1.0); } </pre><hr /> <p> Compiler: </p> <pre class="wiki">$ g++ --version g++ (Ubuntu 5.4.0-6ubuntu1~16.04.4) 5.4.0 20160609 Copyright (C) 2015 Free Software Foundation, Inc. </pre> en-us Boost C++ Libraries /htdocs/site/boost.png https://svn.boost.org/trac10/ticket/13011 Trac 1.4.3 nigels.com@… Sat, 06 May 2017 00:19:49 GMT <link>https://svn.boost.org/trac10/ticket/13011#comment:1 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/13011#comment:1</guid> <description> <p> Seems be due to BOOST_TEST handling of floating point comparison. </p> <p> <a href="http://www.boost.org/doc/libs/1_64_0/libs/test/doc/html/boost_test/testing_tools/extended_comparison/floating_point/floating_points_comparison_theory.html">http://www.boost.org/doc/libs/1_64_0/libs/test/doc/html/boost_test/testing_tools/extended_comparison/floating_point/floating_points_comparison_theory.html</a> </p> <p> This passes, for example: </p> <pre class="wiki">#define BOOST_TEST_MODULE test #include &lt;boost/test/included/unit_test.hpp&gt; BOOST_AUTO_TEST_CASE(Foo) { BOOST_TEST(0.001 &lt; 0.002); } </pre> </description> <category>Ticket</category> </item> <item> <author>chris42f@…</author> <pubDate>Sat, 06 May 2017 00:39:45 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/13011#comment:2 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/13011#comment:2</guid> <description> <p> Right, thanks for digging. Honestly I didn't expect floating point comparison magic in BOOST_TEST by default, and was expecting to do my own epsilon testing. </p> <p> It's unfortunate that the default behavior fails some obvious desired properties at 0.0 - which as the additive identity is arguably the most commonly encountered value out of the entire floating point range. </p> <p> I'll admit that in the real use case, I was trying to do my own epsilon testing with a known absolute tolerance specific to the problem - something like </p> <pre class="wiki">BOOST_TEST(fabs(a-b) &lt; 1e-16) </pre><p> which fails when a happens to be equal to b. Overall, I'm not thrilled that boost tries to completely hide the problems of floating point comparison from users. </p> <p> For this case, it sounds like a solution with the current API is to use the obviously naive expression </p> <pre class="wiki">BOOST_TEST(a == b) </pre> </description> <category>Ticket</category> </item> <item> <author>nigels.com@…</author> <pubDate>Sat, 06 May 2017 02:46:22 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/13011#comment:3 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/13011#comment:3</guid> <description> <p> Yes, I agree that the principle of least-surprise ought to apply to BOOST_TEST. </p> <p> Perhaps we ought to cast to bool as a matter of best practice. </p> <pre class="wiki">BOOST_TEST( bool(fabs(a-b) &lt; 1e-16) ) </pre> </description> <category>Ticket</category> </item> <item> <dc:creator>anonymous</dc:creator> <pubDate>Sat, 06 May 2017 09:18:28 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/13011#comment:4 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/13011#comment:4</guid> <description> <p> Using the bool cast as a matter of course would be terribly unfortunate as it defeats the automatic expression splitting and enhanced error reporting, which is really the whole point of the new BOOST_TEST interface. </p> </description> <category>Ticket</category> </item> <item> <author>nigels.com@…</author> <pubDate>Sat, 06 May 2017 11:47:31 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/13011#comment:5 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/13011#comment:5</guid> <description> <p> My opinion is that the tolerancing built into BOOST_TEST ought to be opt-in. </p> <p> I'll give it another look the next time I'm adding unit testing, but my initial impression is that I'd rather be explicit about epsilon testing, and be a bit less concerned about the precise type to use for the tolerance value. (Ordinarily I'd expect the usual type promotion rules to apply) </p> </description> <category>Ticket</category> </item> <item> <dc:creator>Raffi Enficiaud</dc:creator> <pubDate>Mon, 15 May 2017 10:11:38 GMT</pubDate> <title>owner, status changed https://svn.boost.org/trac10/ticket/13011#comment:6 https://svn.boost.org/trac10/ticket/13011#comment:6 <ul> <li><strong>owner</strong> changed from <span class="trac-author">Gennadiy Rozental</span> to <span class="trac-author">Raffi Enficiaud</span> </li> <li><strong>status</strong> <span class="trac-field-old">new</span> → <span class="trac-field-new">assigned</span> </li> </ul> Ticket Raffi Enficiaud Fri, 23 Jun 2017 16:31:33 GMT milestone changed https://svn.boost.org/trac10/ticket/13011#comment:7 https://svn.boost.org/trac10/ticket/13011#comment:7 <ul> <li><strong>milestone</strong> <span class="trac-field-old">To Be Determined</span> → <span class="trac-field-new">Boost 1.65.0</span> </li> </ul> Ticket Raffi Enficiaud Tue, 27 Jun 2017 18:57:43 GMT <link>https://svn.boost.org/trac10/ticket/13011#comment:8 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/13011#comment:8</guid> <description> <p> Fixed in <code>topic/13011-floating-point-relational-operators</code> </p> </description> <category>Ticket</category> </item> <item> <dc:creator>Raffi Enficiaud</dc:creator> <pubDate>Sun, 16 Jul 2017 08:45:26 GMT</pubDate> <title>status changed; resolution set https://svn.boost.org/trac10/ticket/13011#comment:9 https://svn.boost.org/trac10/ticket/13011#comment:9 <ul> <li><strong>status</strong> <span class="trac-field-old">assigned</span> → <span class="trac-field-new">closed</span> </li> <li><strong>resolution</strong> → <span class="trac-field-new">fixed</span> </li> </ul> <p> In master, rev <code>3ddb0e0d29e8ebfe4ba20921a3d366f7b5b837b2 </code> </p> Ticket