Double comparison in Java

Pavel Polívka - May 12 '21 - - Dev Community

Recently I was solving an interesting bug that came down to comparing two Double variables with equals method. It looks innocent, what can be wrong with something like firstDouble.equals(secondDouble)?
The problem here is with how doubles are stored. To fit them into 64bytes (usually) they are rounded.

See the example below:

Double firstDouble = 0d;
for (int i = 1; i <= 42; i++) {
 firstDouble += 0.1;
}

Double secondDouble = 0.1 * 42;

System.out.println(firstDouble); // 4.200000000000001
System.out.println(secondDouble); // 4.2
System.out.println(firstDouble.equals(secondDouble)); // false
Enter fullscreen mode Exit fullscreen mode

This inaccuracy is caused by rounding errors.
We need to use a different approach to compare those doubles.

Threshold method

If we do not have access to any libraries and want to solve this with Java only we can use something called the threshold method.
Simply, we will subtract those doubles, make absolute value, and compare if the result is smaller than some very small number.

double epsilon = 0.000001d;
boolean result = Math.abs(firstDouble - secondDouble) < epsilon;
System.out.println(result); // true
Enter fullscreen mode Exit fullscreen mode

That small number is called epsilon and the smaller it is the better the accuracy of the result. For most cases, 5 decimals should be enough.

Apache Commons Math

There is no utility method for this in JDK. Luckily for us, Apache Commons Math library has us covered. With it we can compare those doubles like so:

double epsilon = 0.000001d;
boolean result = Precision.equals(firstDouble, secondDouble, epsilon)
System.out.println(result);
Enter fullscreen mode Exit fullscreen mode

The epsilon has the same meaning as in the example above.

There are similar methods in Guava and other libraries.


You can follow me on Twitter to get more tips like this.

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .