This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes long -> float conversion Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "long -> float conversion" Watch "long -> float conversion" New topic
Author

long -> float conversion

Vishy Karl
Ranch Hand

Joined: Sep 08, 2003
Posts: 116
Hello friends,

see the foll. code snippet.
long l = 5; // 1
float f = l; // 2 This compiles fine!!!
l = f; // 3 This gives compiler error !!!
But as we know that long is 64 bits(8 bytes) & float is 32 bits(4 bytes).
Then how is the statement at line 2 valid without any casting??
and statement at line 3 gives error ? I was expecting the behaviour to be the other way.
Thanx in advance.


"The man who can drive himself further once the effort gets painful is the man who will win." <br />Roger Bannister
Yi Meng
Ranch Hand

Joined: May 07, 2003
Posts: 270
Originally posted by Vishy Karl:

But as we know that long is 64 bits(8 bytes) & float is 32 bits(4 bytes).

The meaning for these bits are different. for long, you know that the max is 2^63 - 1 whereas for float, its 32 bits are conform to the IEEE 754 standard. Just google "IEEE 754" and you will find lots of info about the standard and particularly the bit representation. in short, the range that a float can represent is far wider than long ....that's why a long can be implicitly cast to float and not vice versa.
you may just use the link i find for you here. http://research.microsoft.com/~hollasch/cgindex/coding/ieeefloat.html


Meng Yi
 
 
subject: long -> float conversion