Skip to content

Commit

Permalink
Adding proposed fix, with tests, resolves #792
Browse files Browse the repository at this point in the history
  • Loading branch information
sigbjorn committed Sep 19, 2024
1 parent 43112c3 commit 121ffc6
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
16 changes: 14 additions & 2 deletions include/boost/spirit/home/karma/numeric/real_policies.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -257,8 +257,20 @@ namespace boost { namespace spirit { namespace karma
// generate(sink, right_align(precision, '0')[ulong], n);
// but it's spelled out to avoid inter-modular dependencies.

typename remove_const<T>::type digits =
(traits::test_zero(n) ? 1 : ceil(log10(n + T(1.))));
unsigned int digits=1; //should be number of digits n(truncating any fraction)
if(!boost::spirit::traits::test_zero(n)) {
static constexpr uint64_t limit = UINT64_MAX / 10;
const T num = floor(n);
for (uint64_t x = 10u, i = 1u;; x *= 10, i++) {
if (num < x) {
digits=i;break;
}
if (x > limit) {
digits= i + 1;break;
}
}
}

bool r = true;
for (/**/; r && digits < precision_; digits = digits + 1)
r = char_inserter<>::call(sink, '0');
Expand Down
10 changes: 10 additions & 0 deletions test/karma/real3.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,16 @@ int main()
));
}

// test for #792: off by a magnitude due to fraction_part
// notice that it applies to all forms of fractions
// that end with a sequence of 0.009999
{
BOOST_TEST(test("1.09e-06",
double_,
1.09e-06
));
}

return boost::report_errors();
}

0 comments on commit 121ffc6

Please sign in to comment.