Big Moose Saloon
 Search | Java FAQ | Recent Topics Register / Login

# 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
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://aspose.com/file-tools

subject: why -0.0 == 0.0 true ??? and