Bug 251227 - [compiler] Fup of bug 115814, comparing doubles should not be flagged
Summary: [compiler] Fup of bug 115814, comparing doubles should not be flagged
Status: VERIFIED DUPLICATE of bug 281776
Alias: None
Product: JDT
Classification: Eclipse Project
Component: Core (show other bugs)
Version: 3.4   Edit
Hardware: PC Windows XP
: P3 normal (vote)
Target Milestone: 3.6 M1   Edit
Assignee: Srikanth Sankaran CLA
QA Contact:
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2008-10-17 09:46 EDT by Olivier Thomann CLA
Modified: 2010-04-26 14:37 EDT (History)
3 users (show)

See Also:


Attachments
Proposed fix + regression tests (4.05 KB, patch)
2008-10-17 09:48 EDT, Olivier Thomann CLA
no flags Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Olivier Thomann CLA 2008-10-17 09:46:47 EDT
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.
Comment 1 Olivier Thomann CLA 2008-10-17 09:48:06 EDT
Created attachment 115392 [details]
Proposed fix + regression tests

Philippe, please review.
Comment 2 Philipe Mulet CLA 2008-10-29 13:16:55 EDT
What about 1.0 == 1.0  ? 
It feels hard to reject all double comparisons because possibility of a NaN ? 
Comment 3 Srikanth Sankaran CLA 2010-02-24 04:37:09 EST
I will follow up on this, most likely fixed on HEAD.
Comment 4 Srikanth Sankaran CLA 2010-02-25 00:25:52 EST
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 ***
Comment 5 Ayushman Jain CLA 2010-03-08 08:26:08 EST
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?
Comment 6 Srikanth Sankaran CLA 2010-03-09 00:18:46 EST
(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");
			}
		}
		
	}
}
}
Comment 7 Olivier Thomann CLA 2010-04-26 14:37:41 EDT
Verified for 3.6M7 using I20100425-2000