• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Junilu Lacar
  • Liutauras Vilda
Sheriffs:
  • Paul Clapham
  • Jeanne Boyarsky
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
Bartenders:
  • Jesse Duncan
  • Frits Walraven
  • Mikalai Zaikin

long to float conversion

 
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am not clear about the fact that a long could be implicitly converted to float.
float only has 32 bits where as long has 64 bits. How is it possible??
 
Desperado
Posts: 3226
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The short answer is: Because it is. Those are the rules of the language. But they are not arbitrary. As a former so-called scientific programmer, I can say that sometimes the number of significant digits is more important than the absolute maximum and minimum values that a variable can posses.
Meditate upon this and you shall get the answer
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A longer answer: a widening conversion like long to float can indeed involve a loss of precision that can be thought of as round-off error. The long value 1234567890123456789L gets rounded to 1.23456794E18F as a float, or 1.23456789012345677E18 as a double. This is within the possible error you always can have when using floats and doubles, so it should be expected. And it isn't nearly as bad as what can happen for narrowing conversions, when for example the double value 1.2345678901234567E200 gets converted to Float.POSITIVE_INFINITY as a float, 9223372036854775807L as a long, 2147483647 as an int, and -1 as a short or byte. The exact details of how this happens are beyond the scope of our discussion, but the point is - it's not pretty. Widening conversions can result in roundoff error; narrowing conversions can result in complete destruction of any significant digits in the answer - that's why it's treated differently, in most cases forcing the programmer to explicitly cast when narrowing is desired.
 
Anju Rao
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Jim, Tony - thanks!
 
Anju Rao
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Jim, I was referring to this post, where u said narrowing conversions like -1 to short are dangerous? Or did i interpret it wrong?
Thanks,
 
Jim Yingst
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
OK Anju, I've replied to this back here.
 
Yeah, but does being a ninja come with a dental plan? And what about this tiny ad?
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic