• 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

Type Casting

 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
class Cast
{
public static void main(String args[])
{
float f = 1.1f;
float f1= 1.1f;
double f2 ;
f2 = f + f1;
System.out.println(f2);
}
}
Try this program, i was expecting the output as 2.2 but i am getting it as 2.200000047683716. Can anybody explain this???
Thanks,
Subba Rao
 
High Plains Drifter
Posts: 7289
Netbeans IDE VI Editor
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is interesting. I ran this code on the system in front of me, an UltraSPARC 10/Solaris 8 running JDK 1.2.1_04c, and got exactly the same result you did.
I don't have an explanation just yet -- I don't dabble in the numeric types as much as I should -- but I think we can rule out platform-specific issues or some kind of initialization problem.
-----------------
Michael Ernest, co-author of:

    The Complete Java 2 Certification Study Guide


    [This message has been edited by Michael Ernest (edited December 22, 2000).]
 
Ranch Hand
Posts: 1070
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Floats and Doubles have low precision so that the numbers are not always exactly what you would expect them to be after you do mathematical calculations on them.
Bill
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Bill and Michael.
Subba Rao
 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
it is true that a float has a low precision but i am not too sure about the double b'coz this is where double scores over float-accuracy
whenever the literal values are too big or too small the answer in a float datatype might lose accuracy but i think this is not the same in case of double
double shows accurate values.
this is got nothing to do with casting
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic