title: Printing double without losing precision
body: <p>How do you print a double to a stream so that when it is read in you don't lose precision?</p>
  
  <p>I tried:</p>
  
  <pre><code>std::stringstream ss;
  
  double v = 0.1 * 0.1;
  ss &lt;&lt; std::setprecision(std::numeric_limits&lt;T&gt;::digits10) &lt;&lt; v &lt;&lt; " ";
  
  double u;
  ss &gt;&gt; u;
  std::cout &lt;&lt; "precision " &lt;&lt; ((u == v) ? "retained" : "lost") &lt;&lt; std::endl;
  </code></pre>
  
  <p>This did not work as I expected.</p>
  
  <p>But I can increase precision (which surprised me as I thought that digits10 was the maximum required).</p>
  
  <pre><code>ss &lt;&lt; std::setprecision(std::numeric_limits&lt;T&gt;::digits10 + 2) &lt;&lt; v &lt;&lt; " ";
                                                   //    ^^^^^^ +2
  </code></pre>
  
  <p>It has to do with the number of significant digits and the first two don't count in (0.01).</p>
  
  <p>So has anybody looked at representing floating point numbers exactly?
  What is the exact magical incantation on the stream I need to do?</p>
  
  <p>After some experimentation:</p>
  
  <p>The trouble was with my original version. There were non-significant digits in the string after the decimal point that affected the accuracy.</p>
  
  <p>So to compensate for this we can use scientific notation to compensate:</p>
  
  <pre><code>ss &lt;&lt; std::scientific
     &lt;&lt; std::setprecision(std::numeric_limits&lt;double&gt;::digits10 + 1)
     &lt;&lt; v;
  </code></pre>
  
  <p>This still does not explain the need for the +1 though.</p>
  
  <p>Also if I print out the number with more precision I get more precision printed out!</p>
  
  <pre><code>std::cout &lt;&lt; std::scientific &lt;&lt; std::setprecision(std::numeric_limits&lt;double&gt;::digits10) &lt;&lt; v &lt;&lt; "\n";
  std::cout &lt;&lt; std::scientific &lt;&lt; std::setprecision(std::numeric_limits&lt;double&gt;::digits10 + 1) &lt;&lt; v &lt;&lt; "\n";
  std::cout &lt;&lt; std::scientific &lt;&lt; std::setprecision(std::numeric_limits&lt;double&gt;::digits) &lt;&lt; v &lt;&lt; "\n";
  </code></pre>
  
  <p>It results in:</p>
  
  <pre><code>1.000000000000000e-02
  1.0000000000000002e-02
  1.00000000000000019428902930940239457413554200000000000e-02
  </code></pre>
  
  <p>Based on @Stephen Canon answer below:</p>
  
  <p>We can print out exactly by using the printf() formatter, "%a" or "%A". To achieve this in C++ we need to use the fixed and scientific manipulators (see n3225: 22.4.2.2.2p5 Table 88)</p>
  
  <pre><code>std::cout.flags(std::ios_base::fixed | std::ios_base::scientific);
  std::cout &lt;&lt; v;
  </code></pre>
  
  <p>For now I have defined:</p>
  
  <pre><code>template&lt;typename T&gt;
  std::ostream&amp; precise(std::ostream&amp; stream)
  {
      std::cout.flags(std::ios_base::fixed | std::ios_base::scientific);
      return stream;
  }
  
  std::ostream&amp; preciselngd(std::ostream&amp; stream){ return precise&lt;long double&gt;(stream);}
  std::ostream&amp; precisedbl(std::ostream&amp; stream) { return precise&lt;double&gt;(stream);}
  std::ostream&amp; preciseflt(std::ostream&amp; stream) { return precise&lt;float&gt;(stream);}
  </code></pre>
  
  <p>Next: How do we handle NaN/Inf?</p>