Boost logo

Boost Users :

From: Matthijs Möhlmann (matthijs_at_[hidden])
Date: 2020-04-21 19:12:29


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...

Regards, Matthijs





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