Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
May 12, 2021 03:01 pm GMT

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. 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.200000000000001System.out.println(secondDouble); // 4.2System.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.


Original Link: https://dev.to/pavel_polivka/double-comparison-in-java-1b7

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To