I am trying to determine what the maximum precision for a double is. In the comments for the accepted answer in this link Retain precision with double in Java @PeterLawrey states max precision in 15.
How do you determine this ?
I am trying to determine what the maximum precision for a double is. In the comments for the accepted answer in this link Retain precision with double in Java @PeterLawrey states max precision in 15.
How do you determine this ?
That's actually not what he stated at all. What he stated was:
and he is wrong. They have 15 decimal digits of accuracy.
The number of decimal digits in any number is given by its log to the base 10. 15 is the floor value of log10(253-1), where 53 is the number of bits of mantissa (including the implied bit), as described in the Javadoc and IEEE 754, and 253-1 is therefore the maximum possible mantissa value. The actual value is 15.954589770191003298111788092734 to the limits of the Windows calculator.
He is quite wrong to describe it as 'decimal places of accuracy'. A
double
has 15 decimal digits of accuracy if they are all before the decimal point. For numbers with fractional parts you can get many more than 15 digits in the decimal representation, because of the incommensurability of decimal and binary fractions.You could also "measure" it directly:
The idea of this code is to reach the smallest number with one single bit. So you start with
1
(which only has 1 bit) and divide by two (which shifts bits towards the right in binary) until you reach the last bit. The last number printed by this loop is:The max precision of
double
of is the first value greater than 0. According Double's Javadoc, this number is represented byDouble.MIN_VALUE
. You can output it as follows:See this IDEOne program for an example.
Run this code, and see where it stops
Variable
y
becomes the smallest value different than1.0
. On my computer (Mac Intel Core i5), it stops at1.1102...E-16
. It then prints the precision (in decimal and in binary).As stated in https://en.wikipedia.org/wiki/Machine_epsilon, floating-point precision can be estimated with the epsilon value. It is "the smallest number that, when added to one, yields a result different from one" (I did a small variation: 1-e instead of 1+e, but the logic is the same)
I'll explain in decimal: if you have a 4-decimals precision, you can express 1.0000 - 0.0001, but you cannot express the number 1.00000-0.00001 (you lack the 5th decimal). In this example, with a 4-decimals precision, the epsilon is 0.0001. The epsilon directly measures the floating-point precision. Just transpose to binary.
Edit Your question asked "How to determine...". The answer you were searching were more an explanation of than a way to determine precision (with the answer you accepted). Anyways, for other people, running this code on a machine will determine the precision for the "double" type.