Home C++ 浮点数比较
Post
Cancel

C++ 浮点数比较

1
2
3
4
5
const double eps=1e-8; //通常1e-8就可以了
bool equal(double a,double b)
{
    return fabs(a-b)<eps;
}

float.h 头文件中定义了一个常量 FLT_EPSILON, 可以用它作为 eps 的值.

This post is licensed under CC BY 4.0 by the author.