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



LinkBack URL
About LinkBacks


