一个问题,请高手回答(有分)
我发现如果用文件输入(fstream)一个double,
you dont get exactly the double value.
例如,在文件里有一个数字0.5,用
fstream fin;
fin.open("file name");
double temp;
fin>>temp;
这个temp在系统里其实不是0.5,而是0.5000000 0000000 000003
而且如果用cin就没有这个问题
if(temp==0.5)就会是false, in stead of true
我可以很确定不是我的问题
我写了一个很简单的测试程序
#include <iostream>
#include <fstream>
using namespace std;
void main()
{
double a;
double b;
double c;
ifstream fin;
fin.open("proj1.txt");
fin>>a>>b>>c;
cout <<a<<b<<c;
}
proj1.txt is like following:
0.5 0.3 0.2
then i debug the program, i found that the system actually inputted
a=0.5000000 0000000 000000
b=0.2999999 9999999 9999999
b=0.2000000 0000000 0000001
actually system 把0.29999999 999999 999看作了0.3
请问到底为什么?(我用的是VC)