This discussion is archived
1 2 Previous Next 25 Replies Latest reply: Sep 18, 2006 5:48 AM by 807598 Go to original post
• ###### 15. Re: Help with real number input: Is -0.0 < 0.0?
Currently Being Moderated
Same value maybe but internal representation is different. (The bit is present or not).

Consider that
-1 + 1 = 0
Now is that + 0 or -0?

And what does -0 + 0 equal?

Just add 0 (+0) to whatever you are working with and test for the sign using some suitable method.
``````double neg = -0.0;
double result = neg + 0.0;
System.out.println(result==neg);``````
• ###### 16. Re: Help with real number input: Is -0.0 < 0.0?
Currently Being Moderated
``````double pos = 0.0;
double neg = -0.0;

System.out.println(pos == neg);
System.out.println(Double.compare(pos, neg));``````
So, whether the two are considered equal depends on how they are compared. Check the API for Double.equals
• ###### 17. Re: Help with real number input: Is -0.0 < 0.0?
Currently Being Moderated
Same value maybe but internal representation is
different. (The bit is present or not).
If that's addressed at me, I--and the post I was responding to--was talking about real numbers, not Java's representation.

``````double neg = -0.0;
double result = neg + 0.0;
System.out.println(result==neg);``````
That will be true regardless of whether it's +0.0 or -0.0, since -0.0 == +0.0.
• ###### 18. Re: Help with real number input: Is -0.0 < 0.0?
Currently Being Moderated
That will be true regardless of whether it's +0.0 or
-0.0, since -0.0 == +0.0.
Try it with Double instead of double.
• ###### 19. Re: Help with real number input: Is -0.0 < 0.0?
Currently Being Moderated
Interesting.
• ###### 20. Re: Help with real number input: Is -0.0 < 0.0?
Currently Being Moderated
mathematically there's a difference between -0.0 and +0.0 (though it's rather obscure, having to do mostly with asymptotic curves approaching 0 from below and above never touching).
In computer floating point arithmetic there could very well also be as the bit representation might well be different, leading to rounding differences.
• ###### 21. Re: Help with real number input: Is -0.0 < 0.0?
Currently Being Moderated
mathematically there's a difference between -0.0 and
+0.0 (though it's rather obscure, having to do mostly
with asymptotic curves approaching 0 from below and
above never touching).
Eh? 0.0 is a specific value. How does putting a + or - in front of it change that?

And is there a difference between 0.0 and +0.0?
• ###### 22. Re: Help with real number input: Is -0.0 < 0.0?
Currently Being Moderated
it's to do with asymptotically approaching 0 without ever reaching it.
At some point the difference between your actual value and 0 becomes too small to keep track of but it will never be 0.
More of a notation issue than anything else :)
• ###### 23. Re: Help with real number input: Is -0.0 < 0.0?
Currently Being Moderated
More of a notation issue than anything else :)
True, but read my reply #4; +0 and -0 are different in certain circumstances.

kind regards,

Jos
• ###### 24. Re: Help with real number input: Is -0.0 < 0.0?
Currently Being Moderated
you were however talking about computing, not mathematics ;)

Mathematically there is a difference between (+)0 and -0, and in some fields comparing the two would indeed yield a false result.
• ###### 25. Re: Help with real number input: Is -0.0 < 0.0?
Currently Being Moderated
``````public class Test4
{
public static void main(String[] args)
{
double neg = -0.0;
double pos = 0.0;
double result = 1;
result = neg + pos;

if(neg==pos)
System.out.println("neg and pos are the same");
else
System.out.println("neg and pos are not the same");

if(result == pos)
System.out.println("result and pos are the same");
else if(result == neg)
System.out.println("neg and result are the same");

}
}``````
neg and pos are the same
result and pos are the same
So adding -0 to +0 gives +0.
1 2 Previous Next