Stop Using == for Floats: How to Compare Decimals Safely in Java
Stop Using == for Floats: How to Compare Decimals Safely in Java
Want to learn System Design in Depth? Join ByteByteGo now for visual learning. They are also offering a rare 50% discount now on their lifetime plan
If you’ve been programming in Java for a while, you’ve probably heard this advice:
Never compare floating-point numbers using ==.
This is one of the first lessons Java developers learn, yet it’s also one of the most misunderstood.
Many developers know that comparing double or float values using the equality operator can produce unexpected results, but they don’t always know why or what the correct alternative is.
In this article, we’ll look at:
Why == doesn’t work reliably for floating-point numbers
How it can even cause infinite loops
Why Double.equals() isn’t the solution
The proper way to compare floating-point values
Best practices for loops involving floating-point numbers
Let’s dive in.
By the way, if you know Java but truly want to master it or want to take your Java skills to the next level, then I highly recommend you join the Java Specialists Superpack by Java champion Heinz Kaubtz. It’s a great course for Java developers.
Why == Doesn’t Work with Floating-Point Numbers?
Unlike integers, floating-point numbers are stored using the IEEE 754 binary floating-point format.
Many decimal values cannot be represented exactly in binary.
For example:
0.1
0.2
0.3
1/3
are all approximations.
For example,
java
double a = 0.1;
double b = 0.2;
System.out.println(a + b);prints something similar to:
0.30000000000000004
Now consider:
java
double a = 0.1 + 0.2;
System.out.println(a == 0.3);Output:
false
Even though mathematically both values should be equal.
That’s because Java is comparing the exact binary representation.
The Infinite Loop Problem
One common mistake is using != or == inside a loop.
For example:
java
for (double balance = 10.0; balance != 0.0; balance -= 0.1) {
System.out.println(balance);
}At first glance, this looks perfectly reasonable.
The expectation is:
10.0
9.9
9.8
...
0.1
0.0
But that’s not what happens.
Since 0.1 cannot be represented exactly, repeated subtraction accumulates tiny rounding errors.
Eventually the value becomes something like:
0.000000000000002
or
-0.000000000000004
instead of exactly 0.0.
Since the loop condition checks balance != 0.0 it never becomes false. The loop may continue indefinitely.
A Better Solution for Loops
Instead of checking for equality, use a relational operator.
java
for (double balance = 10.0; balance > 0.0; balance -= 0.1) {
System.out.println(balance);
}This loop will terminate.
However, there’s still a subtle issue.
Depending on accumulated rounding errors, the final value might be
0.100000000000003
or
0.000000000000002
before becoming negative.
So while the loop ends, the exact last printed value may not be what you expect.
Using an Epsilon Makes the Loop More Robust
A better approach is to stop when the value is close enough to zero.
java
for (double balance = 10.0; balance > 1E-5; balance -= 0.1) {
System.out.println(balance);
}Using a small threshold (called an epsilon) avoids relying on exact equality.
This makes the loop far more reliable.
The correct epsilon depends on your application.
Typical values include:
1E-5
1E-8
1E-10
Choose one that’s appropriate for your data.
Is Double.equals() Any Better?
A common misconception is that you should replace a == b with Double.valueOf(a).equals(Double.valueOf(b)) .
Unfortunately, this doesn’t solve the problem. The equals() method compares the wrapped primitive values exactly.
For example,
Double.valueOf(0.1 + 0.2) .equals(Double.valueOf(0.3))
still returns false
because the underlying double values are still different.
In other words, == and Double.equals() both perform exact comparisons.
Neither handles floating-point rounding errors.
By the way, if you are new to Java, then I also suggest you start with The Complete Java Masterclass; it's a great course to learn Java from scratch, including float and double.
The Correct Way to Compare Floating-Point Numbers
When comparing floating-point numbers produced by calculations, compare the difference between them instead of checking for exact equality.
Here’s a simple helper method.
java
public static boolean approximatelyEqual(double a,
double b,
double epsilon) {
return Math.abs(a - b) < epsilon;
}Now you can write
java
double a = computeA();
double b = computeB();
if (approximatelyEqual(a, b, 1E-10)) {
System.out.println("Equal");
} else {
System.out.println("Not Equal");
}This is the standard technique used in trading applications, pricing logic, finance, scientific computing, numerical analysis, graphics programming, and machine learning.
Choosing the Right Epsilon
One question naturally follows:
How small should epsilon be?
The answer depends on your problem.





