Community
Participate
Working Groups
When comparing a double with itself, we should not report a comparison with the same expression since comparing a double with itself is the only way to detect that this is not Double.NaN. Indeed Double.NaN == Double.NaN returns false. That code pattern exists in the databinding bundle. The method Double.isNaN(double) could be used instead of the direct comparison, but I think this is a valid comparison that doesn't lead to an obvious true or false answer.
Created attachment 115392 [details] Proposed fix + regression tests Philippe, please review.
What about 1.0 == 1.0 ? It feels hard to reject all double comparisons because possibility of a NaN ?
I will follow up on this, most likely fixed on HEAD.
As verified by org.eclipse.jdt.core.tests.compiler.regression.ProgrammingProblemsTest.test0041() we continue to report a diagnostic for 1.0 == 1.0 kind of comparisons. *** This bug has been marked as a duplicate of bug 281776 ***
I still get a "comparing identical expressions" warning with this: Double val = 0.1; if(val == val){} though i dont get a warning if i use double instead of Double. Seems like the fix for bug 281776 is insufficient to tackle the wrapper types. Is this intentional?
(In reply to comment #5) > I still get a "comparing identical expressions" warning with this: > > Double val = 0.1; > if(val == val){} > > though i dont get a warning if i use double instead of Double. Seems like the > fix for bug 281776 is insufficient to tackle the wrapper types. Is this > intentional? (For the record) Yes, it is. The following program demonstrates the fact that var != var idiom is useful for NaN checking only for primitive floating types and not for references of the wrapper types. In the latter case, we are checking whether the two references don't refer to the same object which is patently false and thus the warning is legitimate. public class X { public static void main(String [] args) { { Double d = Double.NaN; if (d != d) { System.out.println("d != d"); } if (d.isNaN()) { System.out.println("d is Nan"); } } { double d = Double.NaN; if (d != d) { System.out.println("d != d"); } if (Double.isNaN(d)) { System.out.println("d is Nan"); } } } } }
Verified for 3.6M7 using I20100425-2000