开发者

Why do I get a weird result for this sum using Android's Java?

开发者 https://www.devze.com 2023-03-10 08:37 出处:网络
So I\'m doing something very simple. I have two instance 开发者_Python百科variables, newX and newY. These are both doubles initialized to 0.0.

So I'm doing something very simple. I have two instance 开发者_Python百科variables, newX and newY. These are both doubles initialized to 0.0.

During an update loop I perform this calculation:

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;

Here are a few iterations of the logging statement, this is what I can't explain:

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

Why does 1.307 + 0.112 = 1.307?? I'm very confused.


Notice the E9. You are looking at very large numbers.

1E9 = 1,000,000,000

so

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

0

精彩评论

暂无评论...
验证码 换一张
取 消