Note that there are some explanatory texts on larger screens.

plurals
  1. POIs it possible to read infinity or NaN values using input streams?
    primarykey
    data
    text
    <p>I have some input to be read by a input filestream (for example):</p> <p><code>-365.269511 -0.356123 -Inf 0.000000</code></p> <p>When I use <code>std::ifstream mystream;</code> to read from the file to some </p> <p><code>double d1 = -1, d2 = -1, d3 = -1, d4 = -1;</code> </p> <p>(assume <code>mystream</code> has already been opened and the file is valid), </p> <p><code>mystream &gt;&gt; d1 &gt;&gt; d2 &gt;&gt; d3 &gt;&gt; d4;</code></p> <p><code>mystream</code> is in the fail state. I would expect </p> <p><code>std::cout &lt;&lt; d1 &lt;&lt; " " &lt;&lt; d2 &lt;&lt; " " &lt;&lt; d3 &lt;&lt; " " &lt;&lt; d4 &lt;&lt; std::endl;</code> </p> <p>to output </p> <p><code>-365.269511 -0.356123 -1 -1</code>. I would want it to output <code>-365.269511 -0.356123 -Inf 0</code> instead.</p> <p>This set of data was output using C++ streams. Why can't I do the reverse process (read in my output)? How can I get the functionality I seek?</p> <p>From MooingDuck:</p> <pre><code>#include &lt;iostream&gt; #include &lt;limits&gt; using namespace std; int main() { double myd = std::numeric_limits&lt;double&gt;::infinity(); cout &lt;&lt; myd &lt;&lt; '\n'; cin &gt;&gt; myd; cout &lt;&lt; cin.good() &lt;&lt; ":" &lt;&lt; myd &lt;&lt; endl; return 0; } </code></pre> <p>Input: <code>inf</code></p> <p>Output: </p> <pre><code>inf 0:inf </code></pre> <p>See also: <a href="http://ideone.com/jVvei">http://ideone.com/jVvei</a></p> <p>Also related to this problem is <code>NaN</code> parsing, even though I do not give examples for it.</p> <p>I added to the accepted answer a complete solution on ideone. It also includes paring for "Inf" and "nan", some possible variations to those keywords that may come from other programs, such as MatLab.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload