Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

float to long typecasting

 
Sandeep Kumar.
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

i don't know how java stores floating point numbers in memory.(distribution of absolute and fraction digits)
a long takes 8 byte while a float takes 4 bytes only so why can't we typecast a float to a long implicitly. why widening isn't followed here in this case

and why a long can be type casted to a float implicitly while long takes 4 bytes more than float so there must be a loss of precision in this case; still it is allowed.
 
Henry Wong
author
Marshal
Pie
Posts: 21507
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Whether casting can be done implicitedly depends on the range -- not on the size in bytes or the precision. And since the range of the float is larger than the range of the long, a long can be casted to a float implicitedly, even though you may lose precision by doing so.

Henry
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic