Boost C++ Libraries: Ticket #6571: lexical_case<float>(someString) does not recognize IEEE exceptions https://svn.boost.org/trac10/ticket/6571 <p> When using lexical_cast to cast a floating point value (float, double, long double), lexical_cast will generate the proper IEEE floating-point exception string. But when converting the string representation to the real floating point value, lexical_cast doesn't recognize the special values and throws bad_lexical_cast. Look at this example trying to work with double undefined values. The same problem exists for infinity (and probably for other special values that I'm not aware of). </p> <pre class="wiki">#include &lt;boost/lexical_cast.hpp&gt; int main() { boost::lexical_cast&lt;string&gt;(sqrt(-1.0)); //OK, returns "-1.#IND" boost::lexical_cast&lt;double&gt;("-1.#IND"); //bad_lexical_cast !!! return 0; } </pre> en-us Boost C++ Libraries /htdocs/site/boost.png https://svn.boost.org/trac10/ticket/6571 Trac 1.4.3 Chromatix <mhazadmanesh2009@…> Fri, 17 Feb 2012 16:37:21 GMT summary changed https://svn.boost.org/trac10/ticket/6571#comment:1 https://svn.boost.org/trac10/ticket/6571#comment:1 <ul> <li><strong>summary</strong> <span class="trac-field-old">lexical_case&lt;double&gt;(someString) does not recognize IEEE exceptions</span> → <span class="trac-field-new">lexical_case&lt;float&gt;(someString) does not recognize IEEE exceptions</span> </li> </ul> Ticket Antony Polukhin Sat, 18 Feb 2012 08:52:26 GMT status changed https://svn.boost.org/trac10/ticket/6571#comment:2 https://svn.boost.org/trac10/ticket/6571#comment:2 <ul> <li><strong>status</strong> <span class="trac-field-old">new</span> → <span class="trac-field-new">assigned</span> </li> </ul> <p> I`ve tested on GCC compiler, and </p> <pre class="wiki">boost::lexical_cast&lt;string&gt;(sqrt(-1.0)); </pre><p> returns "-nan", which is correct according to standard.<br /> <br /> Please, tell me about the compiler you are using. </p> Ticket Chromatix <mhazadmanesh2009@…> Sat, 18 Feb 2012 10:22:35 GMT <link>https://svn.boost.org/trac10/ticket/6571#comment:3 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/6571#comment:3</guid> <description> <p> As my comments in the example code show, converting to string or char* is always OK. Problem arises when you want to <em>convert the string representation of the error into a floating point type</em> (say, double). For example trying to lexical_cast the above result ("-nan" in GCC, "-1.#IND" in MSVC) to a double throws bad_lexical_cast instead of returning a double containing that special value. </p> </description> <category>Ticket</category> </item> <item> <dc:creator>Antony Polukhin</dc:creator> <pubDate>Mon, 20 Feb 2012 16:37:17 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/6571#comment:4 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/6571#comment:4</guid> <description> <p> I`ve tested boost_1.48 on VC10 and boost::lexical_cast&lt;string&gt;(sqrt(-1.0)); output is '-nan'.<br /> <br /> "-1.#IND" is not a standard, but rather a compiler specific extension. Lexical_cast will not support it.<br /> <br /> You can use lexical_cast everywhere and have a cross platform portable behavior. Or you can wrap around lexical_cast function and use that wrapper to support compiler extensions, violating standard. </p> </description> <category>Ticket</category> </item> <item> <author>Chromatix <mhazadmanesh2009@…></author> <pubDate>Thu, 23 Feb 2012 20:28:14 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/6571#comment:5 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/6571#comment:5</guid> <description> <p> I'm not specifically speaking about <strong>-1.#IND</strong>. lexical_cast fails to convert <strong>-nan</strong> and <strong>inf</strong> from a string type to their floating point values too. </p> <p> If lexical_cast is able to successfully convert the double <code>undefined</code> value to its string representation (<code>-1.#IND</code>, <code>-nan</code>, etc) then it must also be able to do the vice versa without throwing errors, No? </p> <p> If you're wondering why it makes sense to implement such a conversion: I'm working on a program that frequently reads/writes double's to/from file. In the program calculations, sometimes undefined or infinity values are generated (which are fine and sometimes necessary in the program's logic). Now I have no problem saving them to disk (since <strong><code> lexical_cast&lt;string&gt;(someUndefinedDouble) </code></strong> works) but can't read them back (since e.g <strong><code> lexical_cast&lt;double&gt;("-nan") </code></strong> fails). </p> </description> <category>Ticket</category> </item> <item> <dc:creator>Antony Polukhin</dc:creator> <pubDate>Fri, 24 Feb 2012 04:03:42 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/6571#comment:6 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/6571#comment:6</guid> <description> <p> Lets start from the beginning:<br /> 1) I`ve tested lexical_cast&lt;double&gt;("-nan") on VC10 and it passed<br /> 2) I`ve tested boost::lexical_cast&lt;string&gt;(sqrt(-1.0)); on VC10 and it passed and outputteed "-nan"<br /> 3) There are about 90 tests on nan/inf and they pass: <a class="ext-link" href="http://svn.boost.org/svn/boost/branches/release/libs/conversion/test/lexical_cast_inf_nan_test.cpp"><span class="icon">​</span>tests</a>, <a href="http://www.boost.org/development/tests/release/developer/conversion.html">results1</a>, <a href="http://www.boost.org/development/tests/trunk/developer/conversion.html">results2</a>. And they are passing since boost_1.48<br /> <br /> I can not reproduce your error, so please can you answer the following questions:<br /> 1) Which version of boost are you using exactly? Is it boost_1.47, is it boost_1.48?<br /> 2) Which compiler exactly are you using? Is it Visual Studio 2010, is it Visual Studio 2003?<br /> 3) Reproduce the error on your compilerand copypaste the code that produces error to this ticket. </p> </description> <category>Ticket</category> </item> <item> <author>Chromatix <mhazadmanesh2009@…></author> <pubDate>Fri, 24 Feb 2012 10:23:10 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/6571#comment:7 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/6571#comment:7</guid> <description> <p> Oops, it seems I mixed up boost versions. </p> <p> My boost on Windows was 1.47 which is why <code>boost::lexical_cast&lt;string&gt;(sqrt(-1.0))</code> returned <code>-1.#IND</code> on Visual Studio (it's 2010 btw). On 1.48 it returns <code>-nan</code> and can be converted back to double.<br /> So a major part of this bug report doesn't make sense now. Sorry about the mistake here. :( </p> <p> However, the title question still makes sense about <code>-1.#IND</code>. Neither <code>nan</code> nor <code>-1.#IND</code> are global standards. <code>-nan</code> is the standard on *nix (and GCC on windows), but all Microsoft compilers (since VS5 and even before that I guess) represent the error value as <code>-1.#IND</code>, <code>1.#QNAN</code>, etc. (Simply try <code>cout &lt;&lt; sqrt(-1.0)</code>). So I think it's essential to implement such a conversion: <code>boost::lexical_cast&lt;double&gt;("-1.#IND")</code> to work. Since with a Microsoft compiler one might occasionally encounter the <code>-1.#IND</code> and other error strings generated by conversion methods other than boost::lexical_cast or from external libraries. </p> <p> I also found this: <a class="ext-link" href="http://en.wikipedia.org/wiki/NaN#Display"><span class="icon">​</span>http://en.wikipedia.org/wiki/NaN#Display</a>. </p> </description> <category>Ticket</category> </item> <item> <dc:creator>Antony Polukhin</dc:creator> <pubDate>Sun, 26 Feb 2012 09:15:54 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/6571#comment:8 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/6571#comment:8</guid> <description> <p> There is a formal specification for Not-A-Number strings in C. According to standard strtod, strtof, and strtold, scanf, sscanf and fscanf functions must parse NaN like that: </p> <pre class="wiki">The expected form of the subject sequence is an optional plus or minus sign, then one of the following: — INF or INFINITY, ignoring case — NAN or NAN(n-char-sequenceopt), ignoring case in the NAN part, where: ... </pre><p> C++ stream operators use those functions for conversion. <br /> <br /> I`ll add your questions to the lexical_cast documentation (FAQ section). </p> </description> <category>Ticket</category> </item> <item> <dc:creator>Antony Polukhin</dc:creator> <pubDate>Sun, 26 Feb 2012 09:25:48 GMT</pubDate> <title>status, version changed; resolution set https://svn.boost.org/trac10/ticket/6571#comment:9 https://svn.boost.org/trac10/ticket/6571#comment:9 <ul> <li><strong>status</strong> <span class="trac-field-old">assigned</span> → <span class="trac-field-new">closed</span> </li> <li><strong>version</strong> <span class="trac-field-old">Boost 1.48.0</span> → <span class="trac-field-new">Boost 1.50.0</span> </li> <li><strong>resolution</strong> → <span class="trac-field-new">fixed</span> </li> </ul> <p> Documentation updated in <a class="changeset" href="https://svn.boost.org/trac10/changeset/77118" title="Fixes #6571 (documentation update, added 'why lexical_cast does not ...">r77118</a>. </p> Ticket