Skip to content

Commit

Permalink
Karma: real generator off by a magnitude due to log10 rounding up
Browse files Browse the repository at this point in the history
  • Loading branch information
Kojoley committed Sep 22, 2022
1 parent 5049c39 commit 859fd9c
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
2 changes: 1 addition & 1 deletion include/boost/spirit/home/karma/numeric/real_policies.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ namespace boost { namespace spirit { namespace karma
// but it's spelled out to avoid inter-modular dependencies.

typename remove_const<T>::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');
Expand Down
13 changes: 13 additions & 0 deletions test/karma/real3.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@ namespace boost { namespace spirit { namespace traits
}}}
#endif

struct double_prec16_policy : boost::spirit::karma::real_policies<double>
{
static unsigned int precision(double) { return 16; }
};

///////////////////////////////////////////////////////////////////////////////
int main()
{
Expand Down Expand Up @@ -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<double, double_prec16_policy>(),
0.099999999999999770
));
}

return boost::report_errors();
}

0 comments on commit 859fd9c

Please sign in to comment.