• 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

Float Primitive

 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When we assign float f=5.00 We get a compiler error as default type of floating point number is double and cast is neccessary .

Y a cast is not needed when we say float f = 35/6 ;

Some help needed on this..


Thanks in advance.


Kanchan
 
Ranch Hand
Posts: 168
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Kanchan,

In the above stmt only integer division will be carried out not float division.bcoz both 35 & 6 will be taken as integers & result will be
35 /6 = 5(digits after decimal point will be truncated).so no cast is needed here.

Regards,
Priya.
 
Ranch Hand
Posts: 153
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Kanchan,

I hope that answer by priya helps you..! To add more..!

The resultant of the division is integer that is 5 which can be assigned to float.
If the question was like 35.0/6.0 then u needed a explicit cast as the the default type of answer is 5.0 double in this case.

Cheers..!
 
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
floats, if not explicityly casted, need to be declared with the keyword 'f', correct?

float num = 5.0f (this is legal)

Are floats used very often? I can see from a memory conservation point of view why they would be used, but I don't see them too often in practice.
 
reply
    Bookmark Topic Watch Topic
  • New Topic