# wrapper class

udhaya kanagaraj

Ranch Hand

Posts: 62

M Rama

Ranch Hand

Posts: 91

Mikael Bellec

Greenhorn

Posts: 6

posted 10 years ago

- 0

Hi,

the Float equals method use the native method floatToIntBits to do its comparison.

I take it the result of comparison between Float(-0.0) and Float(0.0) might be different according to the platform implementation of this method.

However this does not seem very logical to me.

Rgds,

Mikael.

the Float equals method use the native method floatToIntBits to do its comparison.

I take it the result of comparison between Float(-0.0) and Float(0.0) might be different according to the platform implementation of this method.

However this does not seem very logical to me.

Rgds,

Mikael.

Joe Sondow

Ranch Hand

Posts: 195

posted 10 years ago

- 0

You've found a slight inconsistency in the language. According to JLS 15.21.1 Numerical Equality Operators == and !=:

However, as Mikael Bellec cleverly noted, Float.equals() uses Float.floatToIntBits() to do its comparison. In the Java doc for Float, it mentions the inconsistency:

At the low level, floatToIntBits() returns a bit sequence starting with 0 for a positive zero value, and floatToIntBits() returns a bit sequence starting with 1 for a negative zero value. The two bit sequences are unequal so the equals() test returns false. The reason the authors chose to do it this way is to ensure that for any pair of Float objects where equals() returns true, the hashCode() return values will be equal. The hashCode() method returns

Positive zero and negative zero are considered equal. Therefore, -0.0==0.0 is true, for example.

However, as Mikael Bellec cleverly noted, Float.equals() uses Float.floatToIntBits() to do its comparison. In the Java doc for Float, it mentions the inconsistency:

...two float values are considered to be the same if and only if the method floatToIntBits(float) returns the identical int value when applied to each.

Note that in most cases, for two instances of class Float, f1 and f2, the value of f1.equals(f2) is true if and only if

f1.floatValue() == f2.floatValue()

also has the value true. However, there are two exceptions:

If f1 and f2 both represent Float.NaN, then the equals method returns true, even though Float.NaN==Float.NaN has the value false.

If f1 represents +0.0f while f2 represents -0.0f, or vice versa, the equal test has the value false, even though 0.0f==-0.0f has the value true.

This definition allows hash tables to operate properly.

--Float.equals() Java doc

At the low level, floatToIntBits() returns a bit sequence starting with 0 for a positive zero value, and floatToIntBits() returns a bit sequence starting with 1 for a negative zero value. The two bit sequences are unequal so the equals() test returns false. The reason the authors chose to do it this way is to ensure that for any pair of Float objects where equals() returns true, the hashCode() return values will be equal. The hashCode() method returns

the integer bit representation, exactly as produced by the method floatToIntBits(float), of the primitive float value represented by this Float object.

--Float.hashCode() Java doc

SCJA 1.0 (98%), SCJP 1.4 (98%)

I agree. Here's the link: http://aspose.com/file-tools |