<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Issue 2403: stof() should call strtof() and wcstof()</title>
<meta property="og:title" content="Issue 2403: stof() should call strtof() and wcstof()">
<meta property="og:description" content="C++ library issue. Status: C++17">
<meta property="og:url" content="https://cplusplus.github.io/LWG/issue2403.html">
<meta property="og:type" content="website">
<meta property="og:image" content="http://cplusplus.github.io/LWG/images/cpp_logo.png">
<meta property="og:image:alt" content="C++ logo">
<style>
  p {text-align:justify}
  li {text-align:justify}
  pre code.backtick::before { content: "`" }
  pre code.backtick::after { content: "`" }
  blockquote.note
  {
    background-color:#E0E0E0;
    padding-left: 15px;
    padding-right: 15px;
    padding-top: 1px;
    padding-bottom: 1px;
  }
  ins {background-color:#A0FFA0}
  del {background-color:#FFA0A0}
  table.issues-index { border: 1px solid; border-collapse: collapse; }
  table.issues-index th { text-align: center; padding: 4px; border: 1px solid; }
  table.issues-index td { padding: 4px; border: 1px solid; }
  table.issues-index td:nth-child(1) { text-align: right; }
  table.issues-index td:nth-child(2) { text-align: left; }
  table.issues-index td:nth-child(3) { text-align: left; }
  table.issues-index td:nth-child(4) { text-align: left; }
  table.issues-index td:nth-child(5) { text-align: center; }
  table.issues-index td:nth-child(6) { text-align: center; }
  table.issues-index td:nth-child(7) { text-align: left; }
  table.issues-index td:nth-child(5) span.no-pr { color: red; }
  @media (prefers-color-scheme: dark) {
     html {
        color: #ddd;
        background-color: black;
     }
     ins {
        background-color: #225522
     }
     del {
        background-color: #662222
     }
     a {
        color: #6af
     }
     a:visited {
        color: #6af
     }
     blockquote.note
     {
        background-color: rgba(255, 255, 255, .10)
     }
  }
</style>
</head>
<body>
<hr>
<p><em>This page is a snapshot from the LWG issues list, see the <a href="lwg-active.html">Library Active Issues List</a> for more information and the meaning of <a href="lwg-active.html#C++17">C++17</a> status.</em></p>
<h3 id="2403"><a href="lwg-defects.html#2403">2403</a>. <code>stof()</code> should call <code>strtof()</code> and <code>wcstof()</code></h3>
<p><b>Section:</b> 27.4.5 <a href="https://wg21.link/string.conversions">[string.conversions]</a> <b>Status:</b> <a href="lwg-active.html#C++17">C++17</a>
 <b>Submitter:</b> Stephan T. Lavavej <b>Opened:</b> 2014-06-14 <b>Last modified:</b> 2017-07-30</p>
<p><b>Priority: </b>2
</p>
<p><b>View all other</b> <a href="lwg-index.html#string.conversions">issues</a> in [string.conversions].</p>
<p><b>View all issues with</b> <a href="lwg-status.html#C++17">C++17</a> status.</p>
<p><b>Discussion:</b></p>
<p>
<code>stof()</code> is currently specified to call <code>strtod()</code>/<code>wcstod()</code> (which converts the given string to 
<code>double</code>) and then it's specified to convert that <code>double</code> to <code>float</code>. This performs rounding twice, 
which introduces error. Here's an example written up by James McNellis:
<p/>
Consider the following number <code>X</code>:
</p>
<blockquote>
<pre>
1.999999821186065729339276231257827021181583404541015625 (X)
</pre>
</blockquote>
<p>
This number is exactly representable in binary as:
</p>
<blockquote>
<pre>
1.111111111111111111111101000000000000000000000000000001
* ^1st                  ^23rd                        ^52nd
</pre>
</blockquote>
<p>
I've marked the 23rd and 52nd fractional bits. These are the least significant bits for <code>float</code> and <code>double</code>, 
respectively.
<p/>
If we convert this number directly to <code>float</code>, we take the 23 most significant bits:
</p>
<blockquote>
<pre>
1.11111111111111111111110
</pre>
</blockquote>
<p>
The next bit is a one and the tail is nonzero (the 54th fractional bit is a one), so we round up. This gives us the correctly 
rounded result:
</p>
<blockquote>
<pre>
1.11111111111111111111111
</pre>
</blockquote>
<p>
So far so good. But... If we convert <code>X</code> to <code>double</code>, we take the 52 most significant bits:
</p>
<blockquote>
<pre>
1.1111111111111111111111010000000000000000000000000000 (Y)
</pre>
</blockquote>
<p>
The next bit is a zero, so we round down (truncating the value). If we then convert <code>Y</code> to <code>float</code>, we take 
its 23 most significant bits:
</p>
<blockquote>
<pre>
1.11111111111111111111110
</pre>
</blockquote>
<p>
The next bit is a one and the tail is zero, so we round to even (leaving the value unchanged). This is off by 1ulp from the 
correctly rounded result.
</p>

<p><i>[2014-06 Rapperswil]</i></p>

<p>
Marshall Clow will look at this.
</p>

<p><i>[Urbana 2014-11-07: Move to Ready]</i></p>




<p id="res-2403"><b>Proposed resolution:</b></p>
<p>This wording is relative to N3936.</p>

<ol>
<li><p>Change 27.4.5 <a href="https://wg21.link/string.conversions">[string.conversions]</a> p4+p6 as indicated:</p>

<blockquote>
<pre>
float stof(const string&amp; str, size_t* idx = 0);
double stod(const string&amp; str, size_t* idx = 0);
long double stold(const string&amp; str, size_t* idx = 0);
</pre>
<blockquote>
<p>
-4- <i>Effects</i>: <del>the first two</del><ins>These</ins> functions call <ins><code>strtof(str.c_str(), ptr)</code>,</ins> 
<code>strtod(str.c_str(), ptr)</code><ins>,</ins> and <del>the third function calls</del> <code>strtold(str.c_str(), ptr)</code><ins>, 
respectively</ins>. Each function returns the converted result, if any. [&hellip;]
<p/>
[&hellip;]
<p/>
-6- <i>Throws</i>: <code>invalid_argument</code> if <ins><code>strtof</code>,</ins> <code>strtod</code><ins>,</ins> or <code>strtold</code> reports 
that no conversion could be performed. Throws <code>out_of_range</code> if <ins><code>strtof</code>,</ins> <code>strtod</code><ins>,</ins> or 
<code>strtold</code> sets <code>errno</code> to <code>ERANGE</code> or if the converted value is outside the range of representable 
values for the return type.
</p>
</blockquote>
</blockquote>
</li>

<li><p>Change 27.4.5 <a href="https://wg21.link/string.conversions">[string.conversions]</a> p11+p13 as indicated:</p>

<blockquote>
<pre>
float stof(const wstring&amp; str, size_t* idx = 0);
double stod(const wstring&amp; str, size_t* idx = 0);
long double stold(const wstring&amp; str, size_t* idx = 0);
</pre>
<blockquote>
<p>
-11- <i>Effects</i>: <del>the first two</del><ins>These</ins> functions call <ins><code>wcstof(str.c_str(), ptr)</code>,</ins> 
<code>wcstod(str.c_str(), ptr)</code><ins>,</ins> and <del>the third function calls</del> <code>wcstold(str.c_str(), ptr)</code><ins>, 
respectively</ins>. Each function returns the converted result, if any. [&hellip;]
<p/>
[&hellip;]
<p/>
-13- <i>Throws</i>: <code>invalid_argument</code> if <ins><code>wcstof</code>,</ins> <code>wcstod</code><ins>,</ins> or <code>wcstold</code> 
reports that no conversion could be performed. Throws <code>out_of_range</code> if <ins><code>wcstof</code>,</ins> <code>wcstod</code><ins>,</ins> 
or <code>wcstold</code> sets <code>errno</code> to <code>ERANGE</code>.
</p>
</blockquote>
</blockquote>
</li>
</ol>





</body>
</html>
