• 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

double & float related Qn

 
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm preparing for the SCJP ...

I have a program ....

public class Test {

public static void main(String arg[]){

double d = 1.0f;
double d1 = 1.2f;

double d2 = 1.3f;

System.out.println("d " +d);
System.out.println("d1 " +d1);
System.out.println("d2 " +d2);
}
}

it is printing

d 1.0
d1 1.2000000476837158
d2 1.2999999523162842

why this is so? can anyone explain me.


Sheetal
 
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Because Java floating-point types are IEEE754 types.
http://www.xdweb.net/~dibblego/miscellaneous/documents/IEEE754.pdf
http://qa.jtiger.org/GetQAndA.action?qids=59
 
Sheetal Kaul
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ok, then it should apply for the double also, but if am declaring the value as double d = 1.2d then it is printing 1.2 only, why not 1.2000000476837158 as it is printing if am declaring the double d = 1.2f
 
Sheetal Kaul
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
still I am confused with the difference betn float & double representation. can any one explain me ... please.

double d = 1.2f;
double d1 = 1.2d;

why it is printing different ans, coz both are in the IEEE format.
 
Ranch Hand
Posts: 195
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Sheetal Kaul:
ok, then it should apply for the double also, but if am declaring the value as double d = 1.2d then it is printing 1.2 only, why not 1.2000000476837158 as it is printing if am declaring the double d = 1.2f



Because double can express 1.2 more precisely than float. 1.2f is a rough approximation that's good enough for

float f1 = 1.2f;
System.out.println(f1); // Output: 1.2

But when you assign 1.2f to a double variable, the imprecision of 1.2f becomes apparent when you print the result with double precision.

double d1 = 1.2f;
System.out.println(d1); // Output: 1.2000000476837158

When you use 1.2d it's still an approximation, but it's a closer approximation.

If you don't understand why the computer can't store the value 1.2 perfectly, get out a calculator and try figuring out how 1.2 would be expressed in base-2 (which is binary, the way the computer stores the number).



I only calculated 8 digits past the radix point, giving me
1.00110011...
which is 1.19921875 in base-ten (decimal). If you keep going, you'll gradually get closer to 1.2 but I doubt that you'll ever hit it. Eventually you'll have more binary digits than a double has room for. Remember that double has more digits or precision than float, but it's still finite.
 
Tony Morris
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Because you didn't learn what a IEEE754 floating-point type is
 
Sheetal Kaul
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ya i dont know the complete theory of IEEE format.

but thanks Joe Sanowitz, now i understood the problem with the float & double.


Thanks
Sheetal
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic