• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Junilu Lacar
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Ganesh Patekar
  • Tim Moores
  • Pete Letkeman
  • Stephan van Hulst
Bartenders:
  • Carey Brown
  • Tim Holloway
  • Joe Ess

cast of NaN  RSS feed

 
Ranch Hand
Posts: 247
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear all,
Question 8 of dan's mock exam about primitive conversion:

ANswer : false, false, true.
For the two first answers, the key is to know that before to be casted as short type, a float type has to be casted into an int one.
At first glance, it can appear logical.
But what about the third answer, deduced from the fact that apparently, narrowing cast of Float.NaN or Double.NaN gives 0 as result?
Is there a reason for that or is it just a rule we have to learn?
Thanks in advance for your answer,
Cyril.
 
Ranch Hand
Posts: 456
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is just a guess.
Since NaN is really just a group of bits agreed upon to represent NaN, I would imagine that the agreed upon bit value is actually some sort of fractional value (0.1243...) , and since when you convert float and doubles to integral values, the fractional parts are just truncated off, NaN would become 0, assuming I'm making a correct assumption about NaNs representational value.
For the sake of the exam, I would just remember that it casts to 0, although I doubt it will be on the exam. From what people have told me you can feel confident that K&B's book is inclusive of what you really need for the exam.
 
Ranch Hand
Posts: 1873
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
u can read java.lang.Float API and see what it has to say for NaN,
"The Not-a-Number (NaN) value of type float. It is equal to the value returned by Float.intBitsToFloat(0x7fc00000)"
regards
maulin
 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Logical speculation.
NaN is produced calculating ridiculous values.
Calculation actually produces indeterminate value �Not-a-Number� when zero is divided.
float f = (float)(0.0 / 0.0);
System.out.print(f);
When cast applied to �Not-a-Number� in this case (short), we ask the compiler to give us a number from something that�s Not-a-Number. I think the compiler laughs at this point, and gives us the most logical number 0.
Cyril, I don�t think there's even a remote chance to see this question on the scjp exam.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!