[Boost] [test] strange behaviour when comparing floating point numbers
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
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_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.
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_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_... Regards, Matthijs
participants (2)
-
Edward Diener
-
Matthijs Möhlmann