From 859fd9cf85e2be2ea284690ce914cb5af47159b3 Mon Sep 17 00:00:00 2001 From: Nikita Kniazev Date: Fri, 16 Sep 2022 06:14:36 +0300 Subject: [PATCH] Karma: real generator off by a magnitude due to log10 rounding up --- .../spirit/home/karma/numeric/real_policies.hpp | 2 +- test/karma/real3.cpp | 13 +++++++++++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/include/boost/spirit/home/karma/numeric/real_policies.hpp b/include/boost/spirit/home/karma/numeric/real_policies.hpp index 016abc42cc..a5c7099fba 100644 --- a/include/boost/spirit/home/karma/numeric/real_policies.hpp +++ b/include/boost/spirit/home/karma/numeric/real_policies.hpp @@ -258,7 +258,7 @@ namespace boost { namespace spirit { namespace karma // but it's spelled out to avoid inter-modular dependencies. typename remove_const::type digits = - (traits::test_zero(n) ? 0 : floor(log10(n))) + 1; + (traits::test_zero(n) ? 1 : ceil(log10(n + T(1.)))); bool r = true; for (/**/; r && digits < precision_; digits = digits + 1) r = char_inserter<>::call(sink, '0'); diff --git a/test/karma/real3.cpp b/test/karma/real3.cpp index 0101957bcb..2b6a014503 100644 --- a/test/karma/real3.cpp +++ b/test/karma/real3.cpp @@ -36,6 +36,11 @@ namespace boost { namespace spirit { namespace traits }}} #endif +struct double_prec16_policy : boost::spirit::karma::real_policies +{ + static unsigned int precision(double) { return 16; } +}; + /////////////////////////////////////////////////////////////////////////////// int main() { @@ -185,6 +190,14 @@ int main() BOOST_TEST(test("1.0e-05", double_, 0.00000999999999999998)); } + // test for #735: off by a magnitude due to log10 rounding up + { + BOOST_TEST(test("0.0999999999999998", + karma::real_generator(), + 0.099999999999999770 + )); + } + return boost::report_errors(); }