相关疑难解决方法(0)

将浮点数与三位小数进行比较

我想知道将浮点数比较到三位小数的最快方法.我有类似的东西

float lhs = 2.567xxxx
float rhs = 2.566xxxx
Run Code Online (Sandbox Code Playgroud)

上面应该是不同的,如果它是这样的

float lhs = 2.566xxxx
float rhs = 2.566xxxx
Run Code Online (Sandbox Code Playgroud)

它们应该是一样的

更新:

我正在尝试以下方面

double trunc(double d)
{
    return (d>0) ? floor(d) : ceil(d) ; 
}


bool comparedigits(float a , float b)
{
    if (trunc(1000.0 * a) == trunc(1000.0 * b))
    {
        return true;
    }
    return false;
}

    float g = 2.346;
    float h= 2.34599;
    bool t = comparedigits(g,h) ; //Not the same and should return false;
Run Code Online (Sandbox Code Playgroud)

然而它正在回归真实.

c++ floating-point

3
推荐指数
2
解决办法
8180
查看次数

标签 统计

c++ ×1

floating-point ×1