Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

primitive types assignment  RSS feed

 
Arun Prasath
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
how the float variable is assigned a long variable..?
ex:
long l=1213; //even maximum long value...
float f=l;

float has 4 bytes and long bytes..
how 8 bytes value is assigned to 4 bytes value..?
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rolling back to zero when reaching 4 bytes.
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16028
87
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Indeed, a float is only 32 bits (4 bytes) while a long is 64 bits (8 bytes).

But float is ofcourse a different kind of number - a floating point number instead of an integer. The limits (minimum and maximum values that it can hold) are different. The range of float is even larger than that of float, even though it has less bits!

The trick is that float is less precise - it has only about 6 decimal digits of precision. So you cannot store any number that fits into a long with the same precision into a float.

Try this, for example:

The output is:

123456789
123456792

You see that the float cannot exactly contain the number as the long, there is a rounding error in the last digits.
[ August 29, 2006: Message edited by: Jesper Young ]
 
Srinivas Kalvala
Ranch Hand
Posts: 257
Firefox Browser Hibernate Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

The assignment for float and double values is some what different from just copy the bits. The same bit pattern in long 64 bits area will be treated differently when it is assigned to double for float.

Thank you.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!