• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

how it is possible??????

 
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi..,ranchers
look at this code.....

long l=5555L;
float f=l;
System.out.println(f);

a long takes 64bits where as float takes 32 bytes
then how is it possible to assign a long variable to a float variable???

can anybody explain

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I might be wrong, but doesn't 5555 can be placed in a float?

Also, if the number is too big, doesn't it just get truncated? and you get a warning for a loss of precision?

Like I said, I'm not 100% sure.
 
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Vejey,



a long takes 64bits where as float takes 32 bytes
then how is it possible to assign a long variable to a float variable???



Worry about precision rather than the number of bits consumed..! Just to give you a hint, float uses IEEE XXXX single precision format.

Regards,
Priyanka.
 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
For the numeric primitives narrowest to widest types are
byte->short->int->long->float->double. So, any numeric primitive can be implicitly cast to any primitve wider than itself.

so u can fit a long into a float.
 
Sheriff
Posts: 3063
12
Mac IntelliJ IDE Python VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It's true that a 32 bit floating type can't be relied upon to store a 64 bit integer. In particular, if you run the code ...

... you will find that long1 and long2 aren't always equal. You can lose significant digits, but from the right. When you assign a long to an int, for example, you can potentially chop of bits from the left. That's why the Java compiler complains.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic