happyhelloworld
happyhelloworld

Reputation: 13

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.

Upvotes: 0

Views: 281

Answers (1)

Andy Turner
Andy Turner

Reputation: 140534

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.

Upvotes: 7

Related Questions