Win a copy of The Way of the Web Tester: A Beginner's Guide to Automating Tests this week in the Testing forum!

# precision of double and float

Judy YU
Ranch Hand
Posts: 30
What's the precision of double and float? Could somebody explain?

greg philpott
Ranch Hand
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
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
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
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.