How to determine the max precision for double

20,842

Solution 1

@PeterLawrey states max precision in 15.

That's actually not what he stated at all. What he stated was:

double has 15 decimal places of accuracy

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.

Solution 2

Run this code, and see where it stops

public class FindPrecisionDouble {
  static public void main(String[] args) {
    double x = 1.0;
    double y = 0.5;
    double epsilon = 0;
    int nb_iter = 0;
    while ((nb_iter < 1000) && (x != y)) {
        System.out.println(x-y);
        epsilon = Math.abs(x-y);
        y = ( x + y ) * 0.5;
    }
    final double prec_decimal = - Math.log(epsilon) / Math.log(10.0);
    final double prec_binary = - Math.log(epsilon) / Math.log(2.0);
    System.out.print("On this machine, for the 'double' type, ");
    System.out.print("epsilon = " );
    System.out.println( epsilon );
    System.out.print("The decimal precision is " );
    System.out.print( prec_decimal );
    System.out.println(" digits" );
    System.out.print("The binary precision is " );
    System.out.print( prec_binary );
    System.out.println(" bits" );
  }
}

Variable y becomes the smallest value different than 1.0. On my computer (Mac Intel Core i5), it stops at 1.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.

Share:
20,842
Shivam Sinha
Author by

Shivam Sinha

Updated on April 02, 2020

Comments

  • Shivam Sinha
    Shivam Sinha about 4 years

    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 ?

  • assylias
    assylias about 8 years
    How does that show that doubles have 15 digits precision?
  • Paul
    Paul about 8 years
    I don't think that's true. I believe the OP wants to know the exact number of significant figures that are meaningful in a double. The smallest possible (absolute) double value is just the one with the largest negative exponential portion and the smallest number portion.
  • user207421
    user207421 about 8 years
    The smallest value representable in a double has nothing whatsoever to do with its maximum precision. One is a function of the exponent width, the other of the mantissa width.
  • user207421
    user207421 about 8 years
    You are confusing precision with range, like several other answers here.
  • Sci Prog
    Sci Prog about 8 years
    It tries to find the epsilon value, which is related to the floating-point precision. See en.wikipedia.org/wiki/Machine_epsilon (look at the binary64 line in the table)
  • user207421
    user207421 about 8 years
    He is asking about the floating-point precision itself, not the epsilon, and he is specifically asking about the value 15, which you haven't addressed at all.
  • Sci Prog
    Sci Prog about 8 years
    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. 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. (note: the exponent of my result is 1.11E-16 tells that the precision is about 15 digits)
  • user207421
    user207421 about 8 years
    You should explain all that in your answer, but the number of digits in a decimal fraction tells you exactly nothing. The one you printed has 17 decimal digits.
  • Sci Prog
    Sci Prog about 8 years
    The answer is not the number of decimals digits it prints. The answer is the exponent of the value (-16). I'll remove some digits to remove the confusion.
  • user207421
    user207421 about 8 years
    Again, you needed to explain that in your answer. But all you're providing is an iterative solution to an analytic problem, poorly explained.
  • Shivam Sinha
    Shivam Sinha about 8 years
    thanks clarifies it a bit. However does 2^53 max mantissa value imply 15 digits ?
  • user207421
    user207421 about 8 years
    The number of decimal digits in any number is given by its log to the base 10.
  • Rick Regan
    Rick Regan about 8 years
    The "straight logarithm argument" only applies to integers, which is perhaps what you're saying in your last paragraph. For floating-point you have to do a derivation like I do here: exploringbinary.com/… . For doubles you get the same answer (15); for floats, log_10(2^24-1) is approx 7.22, but the max precision is 6. (Also see stackoverflow.com/questions/30688422/… .)
  • Shivam Sinha
    Shivam Sinha about 8 years
    @RickRegan why is the the max precision 6 and not & for floats ?
  • Rick Regan
    Rick Regan about 8 years
    @ShivamSinha I think that is explained in the two links in my comment.
  • Rick Regan
    Rick Regan almost 8 years
    As a follow up to my prior comment...I wrote an article specifically addressing the issue: exploringbinary.com/… @ShivamSinha