-2

For this code:

double foo = 100.0;
...
// foo may or may not change value here
...
if (foo == 100) {  // will this ever be true?
    ....
}

Will the if block ever be called? If not, what is the proper way to check whether foo == 100.0?

Luiggi Mendoza
  • 85,076
  • 16
  • 154
  • 332
jbreed
  • 1,514
  • 5
  • 22
  • 35

2 Answers2

2

Just give it a try, mate...

public class Test
{
   public static void main(String[] args)
   {
      double foo = 100.0;

      if (foo == 100) 
      {
         System.out.println("true");
      }
   }
}

Output:

true
Eric Postpischil
  • 195,579
  • 13
  • 168
  • 312
JBuenoJr
  • 945
  • 9
  • 14
1

Yes, it can be called. The test can be true. If foo is left alone (or reassigned to 100.0), then the comparison will succeed.

But it will only succeed because 100.0 has an exact representation as a double, and the int value 100 will be converted to the same 100.0 double value via a widening primitive conversion.

You are right to be wary of using == to compare double values, because of the fact that some double values are inexact representations of exact literals (e.g. 0.1 is represented inexactly as a double).

The best way to compare double value is to ensure that the values are within a certain (low) tolerance value of each other, as JUnit does:

assertEquals(double expected, double actual, double delta)

This ensures that the expected and actual values are within the tolerance value delta.

rgettman
  • 176,041
  • 30
  • 275
  • 357
  • 1
    It will not only succeed because 100 has an exact representation as a double. Whatever value is used will be converted to `double` in the same way in the assignment and in the comparison. E.g., `4611686018427387903L` is not exactly representable as a `double`, but the comparison still yields true. – Eric Postpischil Jul 30 '13 at 00:31
  • 3
    Recommending comparing using a tolerance is inappropriate advice because it decreases false reports of inequality at the expense of increasing false reports of equality, and you cannot know whether that is acceptable to an application you know nothing about. The application might be “more interested” in seeking inequality than seeking equality or might have other specifications it needs to meet. – Eric Postpischil Jul 30 '13 at 00:32
  • @EricPostpischil Your comment about how using a tolerance is inappropriate is incorrect; please see http://stackoverflow.com/questions/1088216/whats-wrong-with-using-to-compare-floats-in-java. The same points apply to `double` values. – rgettman Jul 30 '13 at 00:37
  • 3
    What is it in the link you provide that makes you think advising people to use a tolerance is a good idea? All I see is more repetitions of the myth without any explanation of how they deal with the consequences of increasing false reports of equality. There **is** a cost to comparing with a tolerance, it **can** cause applications to fail, and you **cannot** know whether it is good advice for an unknown application. – Eric Postpischil Jul 30 '13 at 00:39
  • 1
    If you use that post to backup your answer, then it would be better to mark the question as dup instead of adding another answer. – Luiggi Mendoza Jul 30 '13 at 00:43