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

posted

0

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

posted

0

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.

With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.