Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

float equality

 
Mert Caliskan
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


why is this outputs false?
 
Joan Horta Tosas
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Because f1 and f2 are two different objects. You have to use equals(). But note that using equals() would give false as well, due to IEEE 754 floating-point "single format" bit layout, as it says in Float.equals() there are two exceptions on having the same result on comparing two float objects and two float values:
� 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.

[ November 03, 2007: Message edited by: Joan Horta Tosas ]
 
Prasun Howlader
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think in the wrapper class negative zero strictly smaller than positive zero.However, the methods Math.min and Math.max treat negative zero as beign smaller that positive zero.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic