java using float comparison will return wrong results

class A
{
    public final static float _EPS = 1E-7f;
    public final static double _EPS2 = 1E-7;

    public static boolean compare(float a, float b)
    {
        return a < b + _EPS;
    }
    public static boolean compare2(float a, float b)
    {
        return a < b + _EPS2;
    }

    public static void main(String [] main)
    {
        float a = 54.124844f;
        float b = 54.124844f;
        System.out.println("compare 1: " + A.compare(a,  b) + " comapre 2: " + A.compare2(a, b));
    }
}

I thought both of these two comparisons will return true, but, the A.compare will return false. The only reason in my mind is because of the range and precision difference between float and double type. However, it seems the number and EPS I used should be within the legal range. Thanks for the help.


This is because 54.124844f + 1e-7f == 54.124844f . Float simply doesn't have enough precision for that addition to yield a value different from 54.124844f .

Using Math.nextAfter(float, double) shows that the next larger value after 54.124844f is 54.124847f . As such, adding a smaller increment than that won't change the float.

The other one returns true because _EPS2 is a double , so b is coerced to a double before the addition. double does have enough precision to represent the difference.

链接地址: http://www.djcxy.com/p/58538.html

上一篇: 为什么Java打开连续整数看起来运行速度更快,增加了一些情况?

下一篇: java使用float比较会返回错误的结果