Why do I get a weird result for this sum using Android's Java?
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
精彩评论