Boost C++ Libraries: Ticket #5796: Casting from float to string using "lexical_cast<string>" gives inaccurate values. https://svn.boost.org/trac10/ticket/5796 <p> When casting floats to strings with lexical_cast&lt;string&gt;() for use in wxTextCtrl, the new boost versions adds a lot of nines or zeroes along with some random numbers at the end of the float values. For example, with an older boost version, I get "0.96" and "0.8" and with new versions, I get "0.959999979" and "0.800000012". </p> <p> I believe this new feature has to do with this problem: </p> <p> "The previous version of lexical_cast used the default stream precision for reading and writing floating-point numbers. For numerics that have a corresponding specialization of std::numeric_limits, the current version now chooses a precision to match." </p> <p> This can easily be fixed by using an older version of lexical_cast.hpp file. Wouldn't it be better to fix this issue, so that I wouldn't need to use the old version of lexical_cast.hpp? </p> <p> I attached the lexical_cast.hpp version with which conversions from floats to strings works. </p> en-us Boost C++ Libraries /htdocs/site/boost.png https://svn.boost.org/trac10/ticket/5796 Trac 1.4.3 Mikko Partonen <mikkopartonen@…> Sun, 21 Aug 2011 09:11:53 GMT attachment set https://svn.boost.org/trac10/ticket/5796 https://svn.boost.org/trac10/ticket/5796 <ul> <li><strong>attachment</strong> → <span class="trac-field-new">lexical_cast.hpp</span> </li> </ul> <p> Working lexical_cast.hpp file. </p> Ticket Antony Polukhin Mon, 22 Aug 2011 15:38:32 GMT <link>https://svn.boost.org/trac10/ticket/5796#comment:1 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/5796#comment:1</guid> <description> <p> Replying to <a class="closed ticket" href="https://svn.boost.org/trac10/ticket/5796" title="#5796: Bugs: Casting from float to string using &#34;lexical_cast&lt;string&gt;&#34; gives ... (closed: wontfix)">Mikko Partonen &lt;mikkopartonen@…&gt;</a>: </p> <blockquote class="citation"> <p> When casting floats to strings with lexical_cast&lt;string&gt;() for use in wxTextCtrl, the new boost versions adds a lot of nines or zeroes along with some random numbers at the end of the float values. For example, with an older boost version, I get "0.96" and "0.8" and with new versions, I get "0.959999979" and "0.800000012".<br /> <br /> </p> </blockquote> <p> It is not a random number, it is the exact representation of float number: <a class="ext-link" href="http://www.parashift.com/c++-faq-lite/newbie.html#faq-29.16"><span class="icon">​</span>http://www.parashift.com/c++-faq-lite/newbie.html#faq-29.16</a> <br /> <br /> </p> <blockquote class="citation"> <p> This can easily be fixed by using an older version of lexical_cast.hpp file. Wouldn't it be better to fix this issue, so that I wouldn't need to use the old version of lexical_cast.hpp? <br /> <br /> </p> </blockquote> <p> Think about all the people, who used an updated version of this library (years passed since 2005, so there will be plenty of them). They relay on current behavior, and such changes will break their code. <br /> <br /> Current behavior was chosen because more people wanted to see precise numbers. When you convert number "1.000000099" in most cases you would like to see number "1.000000099" in output, not "1". </p> <p> You can always use std::stringstream to convert numbers with specified precision. </p> </description> <category>Ticket</category> </item> <item> <dc:creator>Antony Polukhin</dc:creator> <pubDate>Mon, 22 Aug 2011 15:40:02 GMT</pubDate> <title>status changed; cc, resolution set https://svn.boost.org/trac10/ticket/5796#comment:2 https://svn.boost.org/trac10/ticket/5796#comment:2 <ul> <li><strong>cc</strong> <span class="trac-author">antoshkka@…</span> added </li> <li><strong>status</strong> <span class="trac-field-old">new</span> → <span class="trac-field-new">closed</span> </li> <li><strong>resolution</strong> → <span class="trac-field-new">wontfix</span> </li> </ul> Ticket