为什么我使用Android的Java获得这个总和的奇怪结果?

时间:2011-06-05 23:48:07

标签: java android double

所以我做的很简单。我有两个实例变量newX和newY。这些都是双倍初始化为0.0。

在更新循环期间,我执行此计算:

long now = System.currentTimeMillis();
double elapsed = (now - mNextTime)/1000.0; // convert to seconds
Log.i("nx", newX + " + " + elapsed + " = " + (newX + elapsed));
newX = newX + elapsed;
newY  = newY + elapsed;

以下是日志语句的几次迭代,这是我无法解释的:

1.3073173811609962E9 + 0.058 = 1.3073173812189963E9
1.3073173812189963E9 + 0.112 = 1.3073173813309963E9
1.3073173813309963E9 + 0.02 = 1.3073173813509963E9
1.3073173813509963E9 + 0.018 = 1.3073173813689961E9
1.3073173813689961E9 + 0.018 = 1.307317381386996E9
1.307317381386996E9 + 0.101 = 1.307317381487996E9

为什么1.307 + 0.112 = 1.307?我很困惑。

1 个答案:

答案 0 :(得分:7)

注意E9。你看的是非常大的数字。

1E9 = 1,000,000,000

所以

1.3073173811609962E9 + 0.058 = 1,307,317,381.21899623