• 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

Floating Point Literals

 
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
double doubleNumber = 3.4f;


If the above piece of code works,
then the below code script should also work


float floatNumber = 3.4d;

Why is it not working
 
Ranch Hand
Posts: 192
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A double is bigger than a float, so you have to tell the compiler that you know what you're doing with a cast...
float floatNumber = (float) 3.4d;
 
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Pradeep ,

float and double both are to contain floating point numbers .

But width size of float is of 32-bit whereas double is of 64-bit .

When you cast a float to double , You are trying to put smaller stuff
into bigger container . Hence you are safe and there is no potential risk
of loosing precision .

But When you cast a double to float , You are trying to put bigger stuff
into smaller container . Hence there is potential risk of loosing precision . And compiler is smart enough to detect it , that is why it warns you by issuing error .

If you still want to do this , You have to cast it explicitly .
 
Pradeep Balasubramanian
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Friends,
Thanks a lot for your answers

Helping me to think logically
reply
    Bookmark Topic Watch Topic
  • New Topic