很偶然发现了一个精度损失的问题,简单来说:
有表达式: l = i/30 + j/40 + k/25, 求当{i,j,k} = {50,85,27}时l的值,很简单,用计算器马上可以算出答案为4.8717,但是编写了以下程序来实现:
int i = 50,j = 85, k = 27;
double l = i/30 + j/40 + k/25;
运行后,结果显示4.000000000!
int i = 50,j = 85, k = 27;
double l = i/30.0 + j/40.0 + k/25.0;