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.100000001490116120000000000000000000000000000000000.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
我无法从上述结果中回答两个问题,我正在寻求以下方面的帮助:
- 为什么使用的双重表示形式
0.1
,0.2
并且0.3
看起来很精确,而0.1 + 0.2
没有。 - 为什么
0.1f + 0.2f == 0.3f
返回true?
回答:
- 我怀疑在
System.out.printf
这里不能正常工作。double
写入0.1时,获取确切值的一种可靠方法是writenew BigDecimal(0.1).toString()
。 - “为什么0.1f + 0.2f == 0.3f返回true?” 几乎是因为您很幸运:将0.1舍入到最接近的浮点表示形式,将0.2舍入到最接近的浮点表示形式,然后将它们相加,就可以得到最接近的可表示浮点数到0.3。通常情况并非如此,这些价值观恰好起作用。
以上是 Java中的浮点精度和相等性 的全部内容, 来源链接: utcz.com/qa/398334.html