Hello Boost users,
I'm starting to implement some serialization tools with Boost.Json but I've
got some precision issues with double values. To demonstrate this
"potential" issue, you will find here after a code snippet to reproduce it:
#include
BOOST_AUTO_TEST_CASE(bjson_double_serialize_ut)
{
namespace bjs = boost::json;
static double ref_values[] = {
1217.2772861138403
, -161.68713249779881
, 267.04251495962637};
std::string s;
{
const bjs::value jv = {
ref_values[0] , ref_values[1], ref_values[2], 0.0, 0.0, 0.0};
s = serialize(jv);
}
double values[3] = {};
{
const auto jv{bjs::parse(s)};
values[0] = jv.as_array().at(0).as_double();
values[1] = jv.as_array().at(1).as_double();
values[2] = jv.as_array().at(2).as_double();
}
static constexpr auto eps{std::numeric_limits<double>::epsilon()};
BOOST_TEST(std::abs(ref_values[0] - values[0]) <= eps);
BOOST_TEST(std::abs(ref_values[1] - values[1]) <= eps);
BOOST_TEST(std::abs(ref_values[2] - values[2]) <= eps);
}
When I compiling and running this code w/ Boost 1.75 and Microsoft Visual
C++ version 14.2 (VS 2019) / Windows 10, I've got the following output:
"bjson_double_serialize_ut": check std::abs(ref_values[0] - values[0]) <=
eps has failed [2.2737367544323206e-13 > 2.2204460492503131e-16]
"bjson_double_serialize_ut": check std::abs(ref_values[2] - values[2]) <=
eps has failed [5.6843418860808015e-14 > 2.2204460492503131e-16]
From my point of view, I'm expecting to get a tolerance equal or inf. to my
"double machine epsilon". Is this correct and how can I address it ?
Marc
--
Marc Viala