• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

equals method behavior of Float

 
reji singh
Ranch Hand
Posts: 52
  • 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: 21024
78
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • 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
  • 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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Henry and Tomasz
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic