• 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

precision of double and float

 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What's the precision of double and float? Could somebody explain?
 
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
a double can have 14 to 15 significant figures of accuracy.
a float can have 6 to 7 significant figures of accuracy
(see http://www.amazon.com/exec/obidos/ASIN/0130105341/electricporkchop/)
[This message has been edited by greg philpott (edited January 30, 2001).]
 
Ranch Hand
Posts: 186
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A double can represent a value in the range +/- 1.7 * 10^308 ("1.7 times 10 to the 308th power") with 14 or 15 digits of precision. A float can hold +/- 3.4 * 10^38 with 6 or 7 digits of precision. The variability in the digits of precision stems from the fact that "digits of precision" here means in decimal (base 10) but the numbers are stored internally in binary (base 2).
 
Judy YU
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the reply. Actually this question is related with the following program:
public class Float {
public static void main(String[] args) {
float f = 1.0F/3.0F;
System.out.println( f);
System.out.println( (f*3.0f) == 1.0f);
}
}
It print out f as 0.33333334, and true.
I understand that why f is 0.33333334 now since it is a number of 7 significant numbers (the last one doesn't count), but why the last digit is 4 instead of 3.? why wasn't it truncated.
Judy
 
Bill Compton
Ranch Hand
Posts: 186
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, actually I don't know why the last digit is a 4, but my guess is that is has to do with how the binary representation happened to map onto the decimal. I noticed that if you do the same thing with all doubles, the last digit happens to come out 3. I also noticed that the all-double version also reports true, but if you mix float and double, it reports false.
 
Just let me do the talking. Ahem ... so ... you see ... we have this tiny ad...
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic