众所周知,即使是十进制格式的小数点后有固定数字的浮点数也无法准确表示。因此,我有以下程序要测试:
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
我无法从上述结果中回答两个问题,我正在寻求以下方面的帮助:
0.1
0.2
0.3
0.1 + 0.2
0.1f + 0.2f == 0.3f
System.out.printf
double
new BigDecimal(0.1).toString()