streams of double and precision
Hi,
I have a peculiar problem when converting strings of numbers to double.
I open files that contain numbers in lines, such as:
Code:
5.6 0 0.205867 1.0809 7.22644 0.373206 -5.84675e-317 1.99995 0.00433641 1.45331e-45
I want to insert these numbers as double values in a vector of doubles.
My problem is with very small values, lower than 1e-310.
If I use this:
Code:
vector<double> vals;
double x;
while (stream >> x) {
vals.push_back(x);
}
then the vector vals is truncated and contains: 5.6 0 0.205867 1.0809 7.22644 0.373206
-5.84675e-317 is not only eliminated but the while loop ends.
I get the same issue using STL algorithms:
Code:
copy(istream_iterator<double>(stream), istream_iterator<double>(), back_inserter(linevals) );
However, this method works:
Code:
vector<double> vals;
double x;
string st;
while (stream >> st) {
x = atof(st.c_str());
vals.push_back(x);
}
How come atof is able to convert very small values, but not the other methods?
Thanks in advance.