[prev in list] [next in list] [prev in thread] [next in thread] 

List:       boost-users
Subject:    Re: [Boost-users] [Boost] [test] strange behaviour when comparing floating point numbers
From:       Matthijs_Möhlmann_via_Boost-users <boost-users () lists ! boost ! org>
Date:       2020-04-21 20:15:46
Message-ID: bcb4f1b0-8f5a-98de-75ee-0402e865bb23 () cacholong ! nl
[Download RAW message or body]

[Attachment #2 (multipart/signed)]

[Attachment #4 (multipart/mixed)]


On 21/04/2020 22:02, Edward Diener via Boost-users wrote:
> On 4/21/2020 3:12 PM, Matthijs Möhlmann via Boost-users wrote:
> > Hi,
> > 
> > I am trying to compare two floating point numbers but am encountering
> > somewhat
> > strange behaviour.
> > 
> > The minimal compilable example:
> > #define BOOST_TEST_MODULE tests
> > #define BOOST_TEST_DYN_LINK
> > #include <boost/test/unit_test.hpp>
> > 
> > BOOST_AUTO_TEST_SUITE(testsuite)
> > 
> > BOOST_AUTO_TEST_CASE(test1)
> > {
> > namespace tt = boost::test_tools;
> > BOOST_TEST(0.1 == 0.2, tt::tolerance(0.01)); // will fail
> > BOOST_TEST(0.1f == 0.2f, tt::tolerance(0.02)); // will fail too,
> > but different error message...
> > }
> > 
> > BOOST_AUTO_TEST_SUITE_END()
> > 
> > When compiling with:
> > clang++ (clang version 7.0.1-8 (tags/RELEASE_701/final))
> > flags: -o main -g -std=c++17 -stdlib=libc++
> > -lboost_unit_test_framework main.cpp
> > boost: 1.72.0
> > 
> > I'll get the following output:
> > Running 1 test case...
> > main.cpp(10): error: in "testsuite/test1": check 0.1 == 0.2 has
> > failed [0.10000000000000001 != 0.20000000000000001]. Relative
> > difference exceeds tolerance [1 > 0.01]
> > main.cpp(11): error: in "testsuite/test1": check 0.1f == 0.2f has
> > failed [0.100000001 != 0.200000003]
> > 
> > *** 2 failures are detected in the test module "tests"
> > 
> > Why am I getting two different messages? I expected those to be the
> > same...
> 
> My guess, without knowing how Boost Test works, is that the first
> checks two doubles while the second checks two floats and Boost Test
> puts out different messages for each type.
> 
> _______________________________________________
> Boost-users mailing list
> Boost-users@lists.boost.org
> https://lists.boost.org/mailman/listinfo.cgi/boost-users

Thanks you did put me on the right track!

When specifying tt::tolerance(0.01) it will do the check for type
double. I had to do: tt::tolerance(float(0.01)) for the float type.

I should've read the documentation a little bit better:
https://www.boost.org/doc/libs/1_72_0/libs/test/doc/html/boost_test/testing_tools/exte \
nded_comparison/floating_point.html#boost_test.testing_tools.extended_comparison.floating_point.type_of_the_tolerance


Regards, Matthijs


["signature.asc" (application/pgp-signature)]

_______________________________________________
Boost-users mailing list
Boost-users@lists.boost.org
https://lists.boost.org/mailman/listinfo.cgi/boost-users


[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic