boostorg / core

Boost Core Utilities
132 stars 86 forks source link

BOOST_TEST_EQ diagnostic for floating point types lacks precision #89

Open Kojoley opened 3 years ago

Kojoley commented 3 years ago
#include <boost/core/lightweight_test.hpp>
#include <limits>

int main()
{
    BOOST_TEST_EQ(0x9.d495182a99318p-13, 0x9.d495182a99308p-13);
    std::cerr.precision(std::numeric_limits<double>::max_digits10);
    std::cerr << 0x9.d495182a99318p-13 << " == " << 0x9.d495182a99308p-13 << '\n';
    return boost::report_errors();
}
prog.cc(5): test '0x9.d495182a99318p-13 == 0x9.d495182a99308p-13' ('0.0012' == '0.0012') failed in function 'int main()'
0.0012000000000000003 == 0.0011999999999999999
glenfe commented 3 years ago
#include <boost/core/lightweight_test.hpp>
#include <limits>

int main()
{
    BOOST_LIGHTWEIGHT_TEST_OSTREAM.precision(std::numeric_limits<double>::max_digits10);
    BOOST_TEST_EQ(0x9.d495182a99318p-13, 0x9.d495182a99308p-13);
    return boost::report_errors();
}
prog.cc(7): test '0x9.d495182a99318p-13 == 0x9.d495182a99308p-13' ('0.0012000000000000003' == '0.0011999999999999999') failed in function 'int main()'
pdimov commented 3 years ago

It probably makes sense to set the precision before printing floating point, but we need to restore it afterwards, and digits10 is C++11, so it's a bit of a hassle. Although maybe not so much.

pdimov commented 3 years ago

Ah wait, it's not C++11. :-)