3

I have two double values. I want to check if two double values are same or not. I have two way to compare this double values.

First way :

double a = 0.1;
double b = 0.2;
if(a == b) {
    System.out.println("Double values are same");
}

Another way to compare :

if(Double.compare(a, b) == 0) {
    System.out.println("Double values are same");
}

Which one is the best way and accurate? Are both ways the same for comparison double values?

Tunaki
  • 132,869
  • 46
  • 340
  • 423
Shiladittya Chakraborty
  • 4,270
  • 8
  • 45
  • 94

3 Answers3

10

Double.compare handles the case of NaN and negative zeros differently than ==. For Double.compare, -0.0d is not equal to 0.0d.

For example the following code:

public static void main(String... args) {
    double a = -0.0d;
    double b = 0.0d;
    if (a == b) {
        System.out.println("a == b");
    }
    if (Double.compare(a, b) == 0) {
        System.out.println("Double.compare(a, b) == 0");
    }
}

will only print a == b.

The two operations are thus not equivalent. Using one over the other depend on your requirement.

The exact rule concerning == (and !=) can be found in the JLS, section 15.21.1:

Floating-point equality testing is performed in accordance with the rules of the IEEE 754 standard:

  • If either operand is NaN, then the result of == is false but the result of != is true. Indeed, the test x!=x is true if and only if the value of x is NaN.

  • Positive zero and negative zero are considered equal.

  • Otherwise, two distinct floating-point values are considered unequal by the equality operators. In particular, there is one value representing positive infinity and one value representing negative infinity; each compares equal only to itself, and each compares unequal to all other values.

Subject to these considerations for floating-point numbers, the following rules then hold for integer operands or for floating-point operands other than NaN:

  • The value produced by the == operator is true if the value of the left-hand operand is equal to the value of the right-hand operand; otherwise, the result is false.
  • The value produced by the != operator is true if the value of the left-hand operand is not equal to the value of the right-hand operand; otherwise, the result is false.
Tunaki
  • 132,869
  • 46
  • 340
  • 423
  • Also of course interesting to note that sometimes the arithmetic causes doubles that should be equal to not be. – D. Ben Knoble Nov 28 '15 at 13:52
  • Please do not answer duplicates (or typo questions for that matter), you should close instead, as per the [FAQ](http://meta.stackexchange.com/a/10844) – ΦXocę 웃 Пepeúpa ツ May 16 '16 at 19:34
  • @ΦXoce웃Пepeúpa Sure, you will find some if you dig into my history. That is not relevant to _today_. What are you trying to tell me? – Tunaki May 16 '16 at 19:38
3

Let's analyze this part:

if(Double.compare(a, b) == 0)

By looking at the documentation of Double.compare, a possible implementation could be the following one (obviously it will be a more optimized one, but that's for the sake of the discussion):

return a == b ? 0 : (a < b ? -1 : +1);

Then, you have another comparison, so it becomes:

if((a == b ? 0 : (a < b ? -1 : +1))  == 0)

In the other case, you rely on a simple comparison using ==, that is:

if(a == b)

That said, in terms of accuracy I guess the result is the same, for the underlying representation of a double does not change and the comparison with 0 does not seem to affect the accuracy.

Which is the best?

Well, from the example above, I'd say the simpler one for you directly compare the values and you are interested only in equality, even though it's unlikely that you are facing with a problem which will benefit from choosing the best way for such a comparison. Anyway, the approach using Double.compare is more suitable for those cases when you are not only interested in equality, but also in the concepts of greater than and/or less than.

skypjack
  • 49,335
  • 19
  • 95
  • 187
1

Both of those options are perfectly valid for checking the equality of two numbers. I personally would say the == is nicer, but that's just me.

When double.compare() is better is when you want to know why your variables are not equal. It returns a little more information than true or false - a negative value if the left side is smaller, and a positive if the right side is smaller.

Parris Varney
  • 11,320
  • 12
  • 47
  • 76