如何确定双精度的最大精度

How to determine the max precision for double

我正在尝试确定双精度数的最大精度是多少。在此 link Retain precision with double in Java 中接受的答案的评论中 @PeterLawrey 指出 15.

中的最大精度

你是怎么判断的?

您也可以直接"measure":

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

这段代码的思想是用一个位达到最小的数字。因此,您从 1(只有 1 位)开始并除以二(将二进制位向右移动)直到到达最后一位。此循环打印的最后一个数字是:

4.9E-324

运行这段代码,看看它在哪里停止

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" );
  }
}

变量 y 成为不同于 1.0 的最小值。在我的电脑 (Mac Intel Core i5) 上,它停在 1.1102...E-16。然后打印精度(十进制和二进制)。

https://en.wikipedia.org/wiki/Machine_epsilon所述,浮点精度可以用epsilon值估计。 它是"the smallest number that, when added to one, yields a result different from one"(我做了一个小变化:1-e而不是1+e,但逻辑是一样的)

我用十进制解释:如果你有4位小数的精度,你可以表达1.0000 - 0.0001,但你不能表达1.00000-0.00001这个数字(你缺少第5位小数)。在此示例中,精度为 4 位小数,epsilon 为 0.0001。 epsilon 直接测量浮点精度。只需转置为二进制即可。

编辑你问的问题"How to determine..."。您正在搜索的答案更多的是一种解释,而不是一种确定精度的方法(使用您接受的答案)。无论如何,对于其他人来说,运行 机器上的这段代码将决定 "double" 类型的精度。

double的最大精度是第一个大于0的值。根据Double的Javadoc,这个数字用Double.MIN_VALUE表示。可以这样输出:

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

有关示例,请参阅 this IDEOne program

@PeterLawrey states max precision in 15.

实际上他根本不是这么说的。他说的是:

double has 15 decimal places of accuracy

他错了。他们有 15 位小数 的准确性。

任何数的小数位数由其以10为底的对数给出。15是log10(253-1),其中 53 是尾数的位数(包括隐含位),如 Javadoc 和 IEEE 754 中所述,因此 253-1 是最大可能的尾数值。实际值为 15.954589770191003298111788092734 Windows 计算器的极限。

他用'decimal places of accuracy'来形容是完全错误的。一个 double 有 15 个小数 的精度 如果它们都在小数点之前。 对于有小数部分的数字,你可以得到很多由于小数和二进制小数的不可通约性,十进制表示中超过 15 位。