小编典典

Java中的浮点精度和相等性

java

众所周知,即使是十进制格式的小数点后有固定数字的浮点数也无法准确表示。因此,我有以下程序要测试:

public class Main {
  public static void main(String[] args) {
    System.out.printf("0.1 in single precision is %.50f\n", 0.1f);
    System.out.printf("0.2 in single precision is %.50f\n", 0.2f);
    System.out.printf("0.3 in single precision is %.50f\n", 0.3f);
    System.out.printf("0.1 + 0.2 in single precision is %.50f\n", 0.1f + 0.2f);
    System.out.printf("0.1 + 0.2 == 0.3 is %b in single precision\n", 0.1123f * 0.4f + 0.2f * 0.5f == 0.2f * 0.7f + 0.0123f * 0.4f);

    System.out.println();

    System.out.printf("0.1 in double precision is %.50f\n", 0.1);
    System.out.printf("0.2 in double precision is %.50f\n", 0.2);
    System.out.printf("0.3 in double precision is %.50f\n", 0.3);
    System.out.printf("0.1 + 0.2 in double precision is %.50f\n", 0.1 + 0.2);
    System.out.printf("0.1 + 0.2 == 0.3 is %b in double precision\n", 0.1 + 0.2 == 0.3);
  }
}

输出如下:

0.1 in single precision is 0.10000000149011612000000000000000000000000000000000
0.2 in single precision is 0.20000000298023224000000000000000000000000000000000
0.3 in single precision is 0.30000001192092896000000000000000000000000000000000
0.1 + 0.2 in single precision is 0.30000001192092896000000000000000000000000000000000
0.1 + 0.2 == 0.3 is true in single precision

0.1 in double precision is 0.10000000000000000000000000000000000000000000000000
0.2 in double precision is 0.20000000000000000000000000000000000000000000000000
0.3 in double precision is 0.30000000000000000000000000000000000000000000000000
0.1 + 0.2 in double precision is 0.30000000000000004000000000000000000000000000000000
0.1 + 0.2 == 0.3 is false in double precision

我无法从上述结果中回答两个问题,我正在寻求以下方面的帮助:

  1. 为什么使用的双重表示形式0.10.2并且0.3看起来很精确,而0.1 + 0.2没有。
  2. 为什么0.1f + 0.2f == 0.3f返回true?

阅读 388

收藏
2020-11-30

共1个答案

小编典典

  1. 我怀疑在System.out.printf这里不能正常工作。double写入0.1时,获取确切值的一种可靠方法是write new BigDecimal(0.1).toString()
  2. “为什么0.1f + 0.2f == 0.3f返回true?” 几乎是因为您很幸运:将0.1舍入到最接近的浮点表示形式,将0.2舍入到最接近的浮点表示形式,然后将它们相加,就可以得到最接近的可表示浮点数到0.3。通常情况并非如此,这些价值观恰好起作用。
2020-11-30