0
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.

asked Jun 29, 2015 at 15:38
1

1 Answer 1

7

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.

answered Jun 29, 2015 at 15:46

2 Comments

Recommend link, provides a good discussion of the limitations of computers in dealing with floating point numbers.
Hi, Andy, Thanks for the answer. Also, thanks for the link provided by user3745362. The link does provide more insight.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.