Boost logo

Boost Users :

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


I am trying to compare two floating point numbers but am encountering somewhat
strange behaviour.

The minimal compilable example:
#define BOOST_TEST_MODULE tests
#include <boost/test/unit_test.hpp>


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


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, kalb at, bjorn.karlsson at, gregod at, wekempf at