File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes (0.0==-0.0) true or false Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "(0.0==-0.0) true or false" Watch "(0.0==-0.0) true or false" New topic

(0.0==-0.0) true or false

wei liu
Ranch Hand

Joined: Dec 06, 2001
Posts: 56
Question 181)
what does the following expression return?
(0.0 == -0.0)

the ans is true . why?
Manfred Leonhardt
Ranch Hand

Joined: Jan 09, 2001
Posts: 1492
Hi Wei,
This is why. Section 4.2.3 of the JVM Spec:

Positive zero and negative zero compare equal; thus the result of the expression 0.0==-0.0 is true and the result of 0.0>-0.0 is false. But other operations can distinguish positive and negative zero; for example, 1.0/0.0 has the value positive infinity, while the value of 1.0/-0.0 is negative infinity.

I agree. Here's the link:
subject: (0.0==-0.0) true or false
It's not a secret anymore!