Win a copy of Escape Velocity: Better Metrics for Agile Teams this week in the Agile and Other Processes forum!
  • 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
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

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: 3042
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.
 
Think of how dumb the average person is. Mathematically, half of them are EVEN DUMBER. Smart tiny ad:
Garden Master Course kickstarter
https://coderanch.com/t/754577/Garden-Master-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic