Double comparison in Java

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


This content originally appeared on DEV Community and was authored by Pavel Polívka

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

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

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);

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.


This content originally appeared on DEV Community and was authored by Pavel Polívka


Print Share Comment Cite Upload Translate Updates
APA

Pavel Polívka | Sciencx (2021-05-12T15:01:20+00:00) Double comparison in Java. Retrieved from https://www.scien.cx/2021/05/12/double-comparison-in-java/

MLA
" » Double comparison in Java." Pavel Polívka | Sciencx - Wednesday May 12, 2021, https://www.scien.cx/2021/05/12/double-comparison-in-java/
HARVARD
Pavel Polívka | Sciencx Wednesday May 12, 2021 » Double comparison in Java., viewed ,<https://www.scien.cx/2021/05/12/double-comparison-in-java/>
VANCOUVER
Pavel Polívka | Sciencx - » Double comparison in Java. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/05/12/double-comparison-in-java/
CHICAGO
" » Double comparison in Java." Pavel Polívka | Sciencx - Accessed . https://www.scien.cx/2021/05/12/double-comparison-in-java/
IEEE
" » Double comparison in Java." Pavel Polívka | Sciencx [Online]. Available: https://www.scien.cx/2021/05/12/double-comparison-in-java/. [Accessed: ]
rf:citation
» Double comparison in Java | Pavel Polívka | Sciencx | https://www.scien.cx/2021/05/12/double-comparison-in-java/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.