What's wrong with using to compare floats in Java?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Understanding Float Comparisons in Java
While working with data types in Java, particularly floating-point numbers (float and double), developers may encounter unexpected behaviors when using the equality operator (==) to compare these values. This is not unique to Java but is a characteristic of floating-point arithmetic in general. Let's delve into why using == for float comparison can be problematic and explore alternative methods to achieve more reliable outcomes.
Floating-Point Representation
Floating-point numbers in Java are based on the IEEE 754 standard representation. This is a binary representation that allows computers to handle a vast range of values, including very small or very large numbers. However, there are inherent inaccuracies in representing some decimal numbers as floats or doubles, leading to precision errors.
Example of Precision Errors
Consider the following Java code:
Intuitively, one might expect this program to print "Equal". However, it prints "Not Equal" due to precision errors in the representation of these numbers in binary form. Specifically, 0.1f + 0.2f results in a value that is close to, but not exactly, 0.3f in binary representation.
Why == Fails
Using == directly checks if both operands contain the exact same bits, which is often not the case for seemingly identical floating-point computations due to minor precision errors. These inaccuracies make float comparison using == unreliable for arithmetic operations.
Alternatives to == for Float Comparison
To accurately compare floating-point numbers, consider using an epsilon value— a small, predefined tolerance within which the numbers are considered equal.
Epsilon Comparison Example
In this example, rather than checking for equality, we check if the absolute difference between a and b is less than a small epsilon value, allowing for minor discrepancies resulting from floating-point arithmetic.
Key Concepts
The table below summarizes the critical points regarding float comparisons in Java:
| Key Concept | Description |
| Floating-Point Representation | Uses IEEE 754 standard, which introduces precision errors due to binary representation. |
Direct Comparison Issues (==) | Floats might not be exactly equal even when expected to match due to precision errors. |
| Epsilon Value | A small threshold used to compare floats flexibly, allowing minor inaccuracies. |
| Epsilon-Based Comparison | Math.abs(a - b) < epsilon is preferred for comparing two float values for equality. |
Additional Considerations
- Consistent Use of Epsilon: When deciding on an epsilon value, consider the scale of values being compared. Smaller numbers may require a smaller epsilon.
- Financial Calculations: Avoid using floats and doubles in precise calculations like currency. Use
BigDecimalfor these cases, as it provides exact arithmetic without rounding errors. - Performance: Though small, checking with an epsilon involves computational overhead. In performance-critical applications, consider this factor against the need for precision.
Conclusion
In conclusion, understanding the nuances of floating-point representation and arithmetic is crucial for developing reliable Java applications. Rather than relying on ==, developers are encouraged to use an epsilon approach for float comparisons, ensuring their applications handle precision errors gracefully. Recognizing and adapting to these intricacies is essential in producing robust, error-free code when dealing with floating-point operations in Java.
Related reading
- When and how should I use a ThreadLocal variable?
- When and how should I use a ThreadLocal variable?
- When and why do we need ApplicationRunner and Runner interface?
- When and why JPA entities should implement the Serializable interface?
- When do I need to use AtomicBoolean in Java?
- When do Java generics require ? extends T instead of T and is there any downside of switching?
- When do you use Java's @Override annotation and why?
- When do you use varargs in Java?

OOD Fundamentals
Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.