Is it possible to read infinity or NaN values using input streams?
Posted
by
Drise
on Stack Overflow
See other posts from Stack Overflow
or by Drise
Published on 2012-07-10T19:12:22Z
Indexed on
2012/07/10
21:15 UTC
Read the original article
Hit count: 281
c++
|numeric-limits
I have some input to be read by a input filestream (for example):
-365.269511 -0.356123 -Inf 0.000000
When I use std::ifstream mystream;
to read from the file to some
double d1 = -1, d2 = -1, d3 = -1, d4 = -1;
(assume mystream
has already been opened and the file is valid),
mystream >> d1 >> d2 >> d3 >> d4;
mystream
is in the fail state. I would expect
std::cout << d1 << " " << d2 << " " << d3 << " " << d4 << std::endl;
to output
-365.269511 -0.356123 -1 -1
. I would want it to output -365.269511 -0.356123 -Inf 0
instead.
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?
From MooingDuck:
#include <iostream>
#include <limits>
using namespace std;
int main()
{
double myd = std::numeric_limits<double>::infinity();
cout << myd << '\n';
cin >> myd;
cout << cin.good() << ":" << myd << endl;
return 0;
}
Input: inf
Output:
inf
0:inf
See also: http://ideone.com/jVvei
Also related to this problem is NaN
parsing, even though I do not give examples for it.
© Stack Overflow or respective owner