版权声明:如需转载请博客下面留言,并在转载时请标明出处,并附带链接! https://blog.csdn.net/patrick_star_cowboy/article/details/82083392
首先,浮点数在计算机中并不能精确的表示,所以在判断两个double类型的数时需要其他的手段,而不是直接
double temp1 = 0.0, temp2 = 0.0;
if (temp1 == temp2)
{
....
}
最好的是选择一个合适的精度,来进行判断,精度自己决定。
#define length 0.00001
int main()
{
double a = 0.0;
double b = 0.1;
if (fabs(b - a) < length)
{
cout << "right" << endl;
}
else
{
cout << "wrong" << endl;
}
return 0;
}