This week's book giveaway is in the Clojure forum.
We're giving away four copies of Clojure in Action and have Amit Rathore and Francis Avila on-line!
See this thread for details.
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

equals method behavior of Float

 
reji singh
Ranch Hand
Posts: 52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

As per JDK docs

If f1 represents +0.0f while f2 represents -0.0f, or vice versa, the equal test has the value false, //1

even though 0.0f==-0.0f has the value true. //2

Doubt: I agree with statement 1 as -0.0f is not equal to 0.0f but why statement 2 is true? It should not be true as per equality of Objects and primitives.


Thanks
 
Henry Wong
author
Marshal
Pie
Posts: 20836
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Doubt: I agree with statement 1 as -0.0f is not equal to 0.0f but why statement 2 is true? It should not be true as per equality of Objects and primitives.


Statement 2 is true because that is how it is defined by the IEEE spec. In a comparison, negative zero is equal to positive zero.


As for why statement 1 is false, meaning why it violates IEEE specs -- this was done so maps and sets don't consider positive and negative zero as duplicates of each other.

Henry
 
Tom Kowalski
Ranch Hand
Posts: 72
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And don't forget that result depends on whether the variables are Float or float.

 
reji singh
Ranch Hand
Posts: 52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Henry and Tomasz
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic