wood burning stoves*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes precision of double and float Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "precision of double and float" Watch "precision of double and float" New topic
Author

precision of double and float

Judy YU
Ranch Hand

Joined: Nov 19, 2000
Posts: 30
What's the precision of double and float? Could somebody explain?
greg philpott
Ranch Hand

Joined: Nov 10, 2000
Posts: 73
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).]
Bill Compton
Ranch Hand

Joined: Aug 26, 2000
Posts: 186
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

Joined: Nov 19, 2000
Posts: 30
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

Joined: Aug 26, 2000
Posts: 186
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: precision of double and float