Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

comparing MAX Values of Wrapper class with float and double

 
Vishnu Prakash
Ranch Hand
Posts: 1026
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator



o/p ==> equal unequal equal equal equal

Question: Please explain the output
 
Ritesh Raman
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi
vishnu

are you find the answer of your question?
i am still confusing .

plz. explain me the answer--
equal unequal ----
especially with respect to long & int .

with respect
ritesh
 
Ritesh Raman
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi
vishnu

are you find the answer of your question?
i am still confusing .

plz. explain me the answer--
equal unequal ----
especially with respect to long & int .

with respect
ritesh
 
Vishnu Prakash
Ranch Hand
Posts: 1026
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The reason one equality test returns true and the other returns false is that comparing floating-point numbers often has results that are difficult to predict. In this case, you're comparing a float and a double, so the compiler promotes the float to a double in order to do the comparison, because the compiler needs the two values to be the same type before comparing them. Here's a program that illustrates how this is happening:


The output is:

true
true
9.223372036854776E18
9.223372036854776E18
false
false
2.147483648E9
2.147483647E9

When f1 is cast to a double, it happens to have the same exact value as d1, so f1==d1 returns true. However, when f2 is cast to a double, it does not have exactly the same value as d2, so f2==d2 returns false. I don't think there is any way to predict the output without using a compiler, unless you happen to be an expert on floating-point bit patterns.
 
Ritesh Raman
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi
vishnu
i think u r right.

--------------------------------------------------------------------------
there is no any way to predict the output without using a compiler, unless you happen to be an expert on floating-point bit patterns.
---------------------------------------------------------------------------


ritesh
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic