• 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:
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Liutauras Vilda
  • Henry Wong
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Mikalai Zaikin
  • Himai Minh

primitive types assignment

 
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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..?
 
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Rolling back to zero when reaching 4 bytes.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Ranch Hand
Posts: 257
Hibernate Firefox Browser Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic