我好奇地看了一下Emacs的源代码,发现一件惊人的事情!
在C语言中,浮点数是不能用“==”进行比较的,所以我想在C++中也是一样。但是今天偶然蛋疼地写了一下浮点数的比较,发现竟然能直接用“==”比较。各位牛人们,敢问这是为神马?
以下是测试程序:
#include <iostream>
#include <cstdlib>
using namespace std;
int main()
{
double a = 3.1;
if (a == 3.1)
{
cout << "yes" << endl;
}
else
{
cout << "no" << endl;
}
return 0;
}
以下是测试程序:
#include <iostream>
#include <cstdlib>
using namespace std;
int main()
{
double a = 3.1;
if (a == 3.1)
{
cout << "yes" << endl;
}
else
{
cout << "no" << endl;
}
return 0;
}
作者: qgymib 发布时间: 2011-04-02
在C语言中,浮点数是不能用“==”进行比较的,所以我想在C++中也是一样。但是今天偶然蛋疼地写了一下浮点数的比较,发现竟然能直接用“==”比较。各位牛人们,敢问这是为神马?
以下是测试程序:
#include <iostream>
#include <cstdlib>
using namespace std;
int main()
{
double a = 3.1;
if (a == 3.1)
{
cout << "yes" << endl;
}
else
{
cout << "no" << endl;
}
return 0;
}
以下是测试程序:
#include <iostream>
#include <cstdlib>
using namespace std;
int main()
{
double a = 3.1;
if (a == 3.1)
{
cout << "yes" << endl;
}
else
{
cout << "no" << endl;
}
return 0;
}
作者: qgymib 发布时间: 2011-04-02