How to compare two double values in Java?

Java

Java Problem Overview


A simple comparison of two double values in Java creates some problems. Let's consider the following simple code snippet in Java.

package doublecomparision;

final public class DoubleComparision 
{
    public static void main(String[] args) 
    {
        double a = 1.000001;
        double b = 0.000001;

        System.out.println("\n"+((a-b)==1.0));
    }
}

The above code appears to return true, the evaluation of the expression ((a-b)==1.0) but it doesn't. It returns false instead because the evaluation of this expression is 0.9999999999999999 which was actually expected to be 1.0 which is not equal to 1.0 hence, the condition evaluates to boolean false. What is the best and suggested way to overcome such a situation?

Java Solutions


Solution 1 - Java

Basically you shouldn't do exact comparisons, you should do something like this:

double a = 1.000001;
double b = 0.000001;
double c = a-b;
if (Math.abs(c-1.0) <= 0.000001) {...}

Solution 2 - Java

Instead of using doubles for decimal arithemetic, please use java.math.BigDecimal. It would produce the expected results.

For reference take a look at this stackoverflow question

Solution 3 - Java

You can use Double.compare; It compares the two specified double values.

Solution 4 - Java

        int mid = 10;
        for (double j = 2 * mid; j >= 0; j = j - 0.1) {
            if (j == mid) {
            	System.out.println("Never happens"); // is NOT printed
            }
         
            if (Double.compare(j, mid) == 0) {
            	System.out.println("No way!"); // is NOT printed
            }
            
            if (Math.abs(j - mid) < 1e-6) {
            	System.out.println("Ha!"); // printed
            }
        }
        System.out.println("Gotcha!");

Solution 5 - Java

Consider this line of code:

Math.abs(firstDouble - secondDouble) < Double.MIN_NORMAL

It returns whether firstDouble is equal to secondDouble. I'm unsure as to whether or not this would work in your exact case (as Kevin pointed out, performing any math on floating points can lead to imprecise results) however I was having difficulties with comparing two double which were, indeed, equal, and yet using the 'compareTo' method didn't return 0.

I'm just leaving this there in case anyone needs to compare to check if they are indeed equal, and not just similar.

Solution 6 - Java

Just use Double.compare() method to compare double values.
Double.compare((d1,d2) == 0)

double d1 = 0.0;
double d2 = 0.0;

System.out.println(Double.compare((d1,d2) == 0))  // true

Solution 7 - Java

double a = 1.000001;
double b = 0.000001;

System.out.println( a.compareTo(b) );

Returns:

  • -1 : 'a' is numerically less than 'b'.

  • 0 : 'a' is equal to 'b'.

  • 1 : 'a' is greater than 'b'.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionLionView Question on Stackoverflow
Solution 1 - JavaKevinView Answer on Stackoverflow
Solution 2 - JavaZaki SaadehView Answer on Stackoverflow
Solution 3 - JavaAnandView Answer on Stackoverflow
Solution 4 - JavaSplashView Answer on Stackoverflow
Solution 5 - JavaThePC007View Answer on Stackoverflow
Solution 6 - JavaChanaka FernandoView Answer on Stackoverflow
Solution 7 - JavaIbrahimView Answer on Stackoverflow