Comparing floating point numbers in C

后端 未结 4 1970
终归单人心
终归单人心 2020-11-29 10:03

I\'ve got a double that prints as 0.000000 and I\'m trying to compare it to 0.0f, unsuccessfully. Why is there a difference here? What

相关标签:
4条回答
  • 2020-11-29 10:40

    This is fundamental problem with floating point arithmetic on modern computers. They are by nature imprecise, and cannot be reliably compared. For example, the language ML explicitly disallows equality comparison on real types because it was considered too unsafe. See also the excellent (if a bit long and mathematically oriented) paper by David Goldberg on this topic.

    Edit: tl;dr: you might be doing it wrong.

    0 讨论(0)
  • 2020-11-29 10:43

    To determine whether it's close enough to zero that it will print as 0.000000 to six decimal places, something like:

    fabs(d) < 0.0000005
    

    Dealing with small inaccuracies in floating-point calculations can get quite complicated in general, though.

    If you want a better idea what value you've got, try printing with %g instead of %f.

    0 讨论(0)
  • 2020-11-29 10:54

    You can do a range. Like -0.00001 <= x <= 0.00001

    0 讨论(0)
  • 2020-11-29 10:58

    Also, one often overlooked features of floating point number are the denormalized numbers. That's numbers which have the minimal exponent, yet don't fit in the 0.5-1 range.

    Those numbers are lower than FLT_MIN for float, and DBL_MIN for double.

    A common mistake with using a threshold is to compare two values, or use FLT_MIN/DBL_MIN as limit.

    For example, this would lead unlogical result (if you don't know about denormals):

    bool areDifferent(float a, float b) {
        if (a == b) return false;  // Or also: if ((a - b) == FLT_MIN) 
        return true;
    }
    
    
    // What is the output of areDifferent(val, val + FLT_MIN * 0.5f) ?
    // true, not false, even if adding half the "minimum value".
    

    Denormals also usually implies a performance loss in computation. Yet, you can not disable them, else such code could still produce a DIVIDE BY ZERO floating point exception (if enabled):

    float getInverse(float a, float b) {
        if (a != b)
            return 1.0f / (a-b); // With denormals disabled, a != b can be true, but (a - b) can still be denormals, it'll rounded to 0 and throw the exception
        return FLT_MAX;
    }
    
    0 讨论(0)
提交回复
热议问题