aspose file tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes why -0.0 ==  0.0 true ??? and Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Reply Bookmark "why -0.0 ==  0.0 true ??? and " Watch "why -0.0 ==  0.0 true ??? and " New topic
Author

why -0.0 == 0.0 true ??? and

shiren shah
Greenhorn

Joined: Nov 16, 2001
Posts: 14
Hi all,
As we know this order of float/double values
-Infinity --> Negative Numbers/Fractions --> -0.0 --> +0.0 --> Positive Numbers/Fractions --> Infinity.
I could not undestand why
-0.0 == 0.0 // true
but
-0.0< 0.0 // false
and
Math.ceil(-0.0) // -0.0
Math.floor(-0.0) // -0.0
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
This is specified as such in the JLS, so they sure had a reason for deciding so.
JLS 4.2.3 Floating-Point Types, Formats, and Values

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.

HIH


SCJP 5, SCJD, SCBCD, SCWCD, SCDJWS, IBM XML
[Blog] [Blogroll] [My Reviews] My Linked In
Rajinder Yadav
Ranch Hand

Joined: Jan 18, 2002
Posts: 178
As per the Java Language Spec.
Math.ceil() and Math.floor() both differentiate between -0.0. and 0.0


<a href="http://www.rajindery.com" target="_blank" rel="nofollow">Rajinder Yadav</a><p>Each problem that I solved became a rule which served afterwards to solve other problems. --Rene Descartes
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: why -0.0 == 0.0 true ??? and
 
Similar Threads
Math.min()
Unable to Understand Output Of Code
confusion on (==)
extremes of Math.ceil
equals() fails here