According to Java: Negative Zero may or may not be equal to Positive Zero

Last week I was writing some java code involving line slope. I had 3 points (x, y) (3.5, 5) (-1, 5), (8, 5) and I want to verify that these three points are on the same line segment, In order to do that I compared the slope between point 1 and 2 (0/ -4.5 = -0.0) with the slope between point 1 and 3 (0/ 4.5 = 0.0). If i used primitive types this comparison will return true confirming that these points are on the same line segment cause negative zero equals positive zero according to IEEE 754 Standards.

Unfortunately my slope method returns the wrapper class java.lang.Double and comparing negative and positive zeros using the equals method returns false, moreover compareTo method considers positive zero to be greater than negative zero.

I was surprised at first and didn't understand why would I consider negative zero is less than positive zero, but it seems they have a pretty good reason to do so, in some applications you may need to differentiate between negative and positive zeros in spite of their equality they may have a different behavior in other operation e.g. 1.0/0.0 has the value of positive infinity while 1.0/-0.0 is negative infinity. Another reason is allowing hash table to work properly.

Further readings: