17

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 ?

Community
  • 1
  • 1
Shivam Sinha
  • 4,924
  • 7
  • 43
  • 65
  • 3
    Did you look at the [linked wiki in the duplicate question](https://en.wikipedia.org/wiki/Double-precision_floating-point_format)? Java uses IEEE 754 floating-point doubles. – Darth Android Mar 31 '16 at 22:13

4 Answers4

14

@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.

user207421
  • 305,947
  • 44
  • 307
  • 483
  • thanks clarifies it a bit. However does 2^53 max mantissa value imply 15 digits ? – Shivam Sinha Apr 01 '16 at 00:45
  • The number of decimal digits in any number is given by its log to the base 10. – user207421 Apr 01 '16 at 01:04
  • 2
    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: http://www.exploringbinary.com/number-of-digits-required-for-round-trip-conversions/ . 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 http://stackoverflow.com/questions/30688422/is-the-most-significant-decimal-digits-precision-that-can-be-converted-to-binary .) – Rick Regan Apr 01 '16 at 14:54
  • @RickRegan why is the the max precision 6 and not & for floats ? – Shivam Sinha Apr 19 '16 at 15:23
  • @ShivamSinha I think that is explained in the two links in my comment. – Rick Regan Apr 21 '16 at 02:02
  • 1
    As a follow up to my prior comment...I wrote an article specifically addressing the issue: http://www.exploringbinary.com/decimal-precision-of-binary-floating-point-numbers/ @ShivamSinha – Rick Regan Jun 17 '16 at 18:00
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.

Sci Prog
  • 2,651
  • 1
  • 10
  • 18
  • It tries to find the epsilon value, which is related to the floating-point precision. See https://en.wikipedia.org/wiki/Machine_epsilon (look at the binary64 line in the table) – Sci Prog Mar 31 '16 at 23:43
  • 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. – user207421 Mar 31 '16 at 23:46
  • 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) – Sci Prog Mar 31 '16 at 23:56
  • 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. – user207421 Mar 31 '16 at 23:58
  • 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. – Sci Prog Apr 01 '16 at 00:00
  • Again, you needed to explain that in your answer. But all you're providing is an iterative solution to an analytic problem, poorly explained. – user207421 Apr 01 '16 at 00:02
-1

You could also "measure" it directly:

for(double d = 1 ; d > 0 ; d/=2) System.out.println(d);

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:

4.9E-324

Maljam
  • 6,244
  • 3
  • 17
  • 30
  • 4
    How does that show that doubles have 15 digits precision? – assylias Mar 31 '16 at 22:32
  • 2
    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 Mar 31 '16 at 23:28
-1

The max precision of double of is the first value greater than 0. According Double's Javadoc, this number is represented by Double.MIN_VALUE. You can output it as follows:

BigDecimal doubleMinVal = BigDecimal.valueOf(Double.MIN_VALUE);
System.out.println(doubleMinVal.toPlainString());
System.out.println(doubleMinVal.toString());

See this IDEOne program for an example.

entpnerd
  • 10,049
  • 8
  • 47
  • 68
  • 1
    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. – Paul Mar 31 '16 at 23:23
  • You are confusing precision with range, like several other answers here. – user207421 Mar 31 '16 at 23:31