Sunday 15 May 2011

wrong reading numbers from a file c++ -



wrong reading numbers from a file c++ -

i have file matrix n x n, illustration got these numbers:

3.162277660168379 4.110960958218893 7.58946638440411 -1.110223024625157e-016 6.715653356152327 6.224264086189961 -5.551115123125783e-016 -1.332267629550188e-015 3.107818621696872

and got code:

std::ifstream in("r.txt"); std::string line; float b[4][4]; int u = 1, y = 1; while (std::getline(in, line)) { float value; int y = 1; std::stringstream ss(line); while (ss >> value) { b[u][y] = value; cout << setprecision(16) << b[u][y] << " "; ++y; } cout << endl; ++u; }

and reply when seek output in command line:

3.162277698516846 4.110960960388184 7.589466571807861 -1.110223024625157e-016 6.715653419494629 6.224264144897461 -5.551115123125783e-016 -1.332267629550188e-015 3.107818603515625

has any1 thought why numbers wrong readed ? 8th digit ? :) thanks

instead of using float utilize double, because float work upto 8 decimal places

c++

No comments:

Post a Comment