为什么C能够准确地表示浮点1.75和1.05,而不是2.45和0.7?
在下面的代码中,我试图证明我的意思。 1.75 - 1.05 - 0.70正好为0,但1.75 - 2.45 - 0.70不是,但小数位数是相同的。
C代码:
int digs = DECIMAL_DIG;
printf("1.75 - 1.05 -0.70: %.*e \n",digs, A1-A3-A4);
printf("1.75 - 2.45 - 0.70: %.*e \n",digs, A1-A2+A4);
printf("\n2.45: %.*e \n",digs, 2.45);
printf("\n1.75: %.*e \n",digs, 1.75);
printf("\n1.05: %.*e \n",digs, 1.05);
printf("\n0.70: %.*e \n",digs, 0.70);
printf("\n1.00: %.*e \n",digs, 1.00);
输出:
1.75 - 1.05 -0.70: 0.000000000000000000000e+000
1.75 - 2.45 - 0.70: -2.220446049250313100000e-016
2.45: 2.450000000000000200000e+000
1.75: 1.750000000000000000000e+000
1.05: 1.050000000000000000000e+000
0.70: 6.999999999999999600000e-001
1.00: 1.000000000000000000000e+000