Why is Java incompatible when comparing -0.0 and +0.0? What is the standard Java method for comparing numbers with a -0 / + 0 account?
I came across this particular bugaboo:
public class ZeroCompare { public static void main(String[] args) { if ( 0.0 == -0.0 ) { System.out.println("== --> same"); } else { System.out.println("== --> different"); } if ( new Double(0.0).equals( -0.0 ) ) { System.out.println("equals --> same"); } else { System.out.println("equals --> different"); } } }
He prints the following:
== --> same equals --> different
I really dislike the fact that how you compare these two values ββaffects the result, and I would like an explanation.
Curds source share