Opened 9 years ago

Last modified 7 years ago

#9405 new Bugs

boost::spirit::karma::real_generator prints a number multiplied by 10

Reported by: Josef Zlomek <josef@…> Owned by: Hartmut Kaiser
Milestone: To Be Determined Component: spirit
Version: Boost 1.49.0 Severity: Problem
Keywords: Cc:

Description

When using the attached example, the number 0.09999999999999987 is printed as 0.999999999999999, i.e. multiplied by 10.

Attachments (1)

spirit_karma_generator_bug.cxx (929 bytes ) - added by Josef Zlomek <josef@…> 9 years ago.
Test that prints the number incorrectly

Download all attachments as: .zip

Change History (3)

by Josef Zlomek <josef@…>, 9 years ago

Test that prints the number incorrectly

comment:1 by Joel de Guzman, 9 years ago

Owner: changed from Joel de Guzman to Hartmut Kaiser

comment:2 by code@…, 7 years ago

I found the source of the issue, its this line here. The call to log10 rounds up to 15 instead of 14.9999....

Fixing the issue is tricky, at least for me since I don't know many floating point tricks. The ideas I have:

  • Divide by 10 in a loop to simulate what the int_inserter is doing below
  • Have int_inserter write to a temporary stack buffer first (with output tracking), and then copy the stack buffer to the real one.
    • The stack buffer would be checked, and would error if it exceeded its bounds
  • Remove the + 1 from this line.
    • Since the value no longer exceeds digits10, the log10 function no longer rounds.
    • Doesn't seem portable or reliable, but I could be mistaken.

Unfortunately I don't know of a way to reliably fix this without a performance impact. I can fix + add tests for this after some guidance.

Note: See TracTickets for help on using tickets.