2
0
mirror of https://github.com/boostorg/spirit.git synced 2026-01-19 04:42:11 +00:00

Karma: real generator off by a magnitude due to log10 rounding up

This commit is contained in:
Nikita Kniazev
2022-09-16 06:14:36 +03:00
parent 5049c39ec5
commit 859fd9cf85
2 changed files with 14 additions and 1 deletions

View File

@@ -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');

View File

@@ -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()
{
@@ -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();
}