Boost C++ Libraries: Ticket #9405: boost::spirit::karma::real_generator prints a number multiplied by 10 https://svn.boost.org/trac10/ticket/9405 <p> When using the attached example, the number 0.09999999999999987 is printed as 0.999999999999999, i.e. multiplied by 10. </p> en-us Boost C++ Libraries /htdocs/site/boost.png https://svn.boost.org/trac10/ticket/9405 Trac 1.4.3 Josef Zlomek <josef@…> Mon, 18 Nov 2013 08:24:50 GMT attachment set https://svn.boost.org/trac10/ticket/9405 https://svn.boost.org/trac10/ticket/9405 <ul> <li><strong>attachment</strong> → <span class="trac-field-new">spirit_karma_generator_bug.cxx</span> </li> </ul> <p> Test that prints the number incorrectly </p> Ticket Joel de Guzman Mon, 18 Nov 2013 09:10:44 GMT owner changed https://svn.boost.org/trac10/ticket/9405#comment:1 https://svn.boost.org/trac10/ticket/9405#comment:1 <ul> <li><strong>owner</strong> changed from <span class="trac-author">Joel de Guzman</span> to <span class="trac-author">Hartmut Kaiser</span> </li> </ul> Ticket code@… Sat, 09 May 2015 00:34:46 GMT <link>https://svn.boost.org/trac10/ticket/9405#comment:2 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/9405#comment:2</guid> <description> <p> I found the source of the issue, its <a class="ext-link" href="https://github.com/boostorg/spirit/blob/master/include/boost/spirit/home/karma/numeric/real_policies.hpp#L263"><span class="icon">​</span>this line</a> here. The call to <code>log10</code> rounds up to <code>15</code> instead of <code>14.9999...</code>. </p> <p> Fixing the issue is tricky, at least for me since I don't know many floating point tricks. The ideas I have: </p> <ul><li>Divide by 10 in a loop to simulate what the <code>int_inserter</code> is doing below </li><li>Have <code>int_inserter</code> write to a temporary stack buffer first (with output tracking), and then copy the stack buffer to the real one. <ul><li>The stack buffer would be checked, and would error if it exceeded its bounds </li></ul></li><li>Remove the <code>+ 1</code> <a class="ext-link" href="https://github.com/boostorg/spirit/blob/master/include/boost/spirit/home/karma/numeric/detail/real_utils.hpp#L91"><span class="icon">​</span>from this line</a>. <ul><li>Since the value no longer exceeds <code>digits10</code>, the <code>log10</code> function no longer rounds. </li><li>Doesn't seem portable or reliable, but I could be mistaken. </li></ul></li></ul><p> 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. </p> </description> <category>Ticket</category> </item> </channel> </rss>