[英]wrong reading numbers from a file c++
我有一個矩陣nxn的文件,例如,我得到了這些數字:
3.162277660168379 4.110960958218893 7.58946638440411 -1.110223024625157e-016 6.715653356152327 6.224264086189961 -5.551115123125783e-016 -1.332267629550188e-015 3.107818621696872
並得到代碼:
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;
}
當我嘗試在命令行中輸出時,得到以下答案:
3.162277698516846 4.110960960388184 7.589466571807861 -1.110223024625157e-016 6.715653419494629 6.224264144897461 -5.551115123125783e-016 -1.332267629550188e-015 3.107818603515625
有人知道為什么我的數字讀錯了嗎? 從第8位開始? :) 謝謝
不要使用float使用double,因為float最多只能使用8個小數位
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.