我有这段代码:
partialProg = 100 * ((i+1) / len);
Log.i(null, "100 * (" + (i+1) + "/" + len + ")" );
Log.i(null, "" + (int) partialProg + " <- int cast original double -> " + partialProg);
其中partialProg
是double,初始化为1.0作为全局变量。 partialProg
用于计算冗长上传过程中的进度(len
是要上传的数据点的数量,i
是循环所在的当前数据点。从逻辑上讲,这是正确的。
但是,我的ProgressDialog
始终显示0%,直到最后一次迭代,其中显示100%。
好奇,我使用Log消息试图找到问题。我得到这样的结果:
11-22 13:21:47.714: ERROR/(3110): LOOP ENTERED: 1 accelX to check for null: -2.329
11-22 13:21:48.044: DEBUG/rapi(3110): Successful request
11-22 13:21:48.044: INFO/global(3110): Default buffer size used in BufferedReader constructor. It would be better to be explicit if an 8k-char buffer is required.
11-22 13:21:48.064: INFO/(3110): 100 * (2/20)
11-22 13:21:48.074: INFO/(3110): 0 <- int cast original double -> 0.0
11-22 13:21:48.104: ERROR/(3110): LOOP ENTERED: 2 accelX to check for null: -10.924
11-22 13:21:48.434: DEBUG/rapi(3110): Successful request
11-22 13:21:48.434: INFO/global(3110): Default buffer size used in BufferedReader constructor. It would be better to be explicit if an 8k-char buffer is required.
11-22 13:21:48.454: INFO/(3110): 100 * (3/20)
11-22 13:21:48.454: INFO/(3110): 0 <- int cast original double -> 0.0
11-22 13:21:48.484: ERROR/(3110): LOOP ENTERED: 3 accelX to check for null: -8.281
11-22 13:21:48.664: DEBUG/rapi(3110): Successful request
11-22 13:21:48.674: INFO/global(3110): Default buffer size used in BufferedReader constructor. It would be better to be explicit if an 8k-char buffer is required.
11-22 13:21:48.694: INFO/(3110): 100 * (4/20)
11-22 13:21:48.694: INFO/(3110): 0 <- int cast original double -> 0.0
11-22 13:21:48.754: ERROR/(3110): LOOP ENTERED: 4 accelX to check for null: -2.438
11-22 13:21:49.334: DEBUG/rapi(3110): Successful request
11-22 13:21:49.334: INFO/global(3110): Default buffer size used in BufferedReader constructor. It would be better to be explicit if an 8k-char buffer is required.
11-22 13:21:49.334: INFO/(3110): 100 * (5/20)
11-22 13:21:49.334: INFO/(3110): 0 <- int cast original double -> 0.0
11-22 13:21:49.364: ERROR/(3110): LOOP ENTERED: 5 accelX to check for null: -9.657
11-22 13:21:49.554: DEBUG/rapi(3110): Successful request
11-22 13:21:49.554: INFO/global(3110): Default buffer size used in BufferedReader constructor. It would be better to be explicit if an 8k-char buffer is required.
11-22 13:21:49.554: INFO/(3110): 100 * (6/20)
11-22 13:21:49.554: INFO/(3110): 0 <- int cast original double -> 0.0
11-22 13:21:49.584: ERROR/(3110): LOOP ENTERED: 6 accelX to check for null: 7.042
11-22 13:21:49.653: DEBUG/rapi(3110): Successful request
11-22 13:21:49.653: INFO/global(3110): Default buffer size used in BufferedReader constructor. It would be better to be explicit if an 8k-char buffer is required.
11-22 13:21:49.653: INFO/(3110): 100 * (7/20)
11-22 13:21:49.653: INFO/(3110): 0 <- int cast original double -> 0.0
11-22 13:21:49.694: ERROR/(3110): LOOP ENTERED: 7 accelX to check for null: -6.347
11-22 13:21:49.814: DEBUG/rapi(3110): Successful request
11-22 13:21:49.814: INFO/global(3110): Default buffer size used in BufferedReader constructor. It would be better to be explicit if an 8k-char buffer is required.
11-22 13:21:49.814: INFO/(3110): 100 * (8/20)
11-22 13:21:49.824: INFO/(3110): 0 <- int cast original double -> 0.0
11-22 13:21:49.844: ERROR/(3110): LOOP ENTERED: 8 accelX to check for null: -15.663
11-22 13:21:49.973: DEBUG/rapi(3110): Successful request
11-22 13:21:49.973: INFO/global(3110): Default buffer size used in BufferedReader constructor. It would be better to be explicit if an 8k-char buffer is required.
11-22 13:21:49.973: INFO/(3110): 100 * (9/20)
11-22 13:21:49.973: INFO/(3110): 0 <- int cast original double -> 0.0
11-22 13:21:50.013: ERROR/(3110): LOOP ENTERED: 9 accelX to check for null: -1.771
11-22 13:21:50.143: DEBUG/rapi(3110): Successful request
11-22 13:21:50.143: INFO/global(3110): Default buffer size used in BufferedReader constructor. It would be better to be explicit if an 8k-char buffer is required.
11-22 13:21:50.143: INFO/(3110): 100 * (10/20)
11-22 13:21:50.154: INFO/(3110): 0 <- int cast original double -> 0.0
11-22 13:21:50.174: ERROR/(3110): LOOP ENTERED: 10 accelX to check for null: 12.326
11-22 13:21:50.304: DEBUG/rapi(3110): Successful request
11-22 13:21:50.304: INFO/global(3110): Default buffer size used in BufferedReader constructor. It would be better to be explicit if an 8k-char buffer is required.
11-22 13:21:50.304: INFO/(3110): 100 * (11/20)
11-22 13:21:50.304: INFO/(3110): 0 <- int cast original double -> 0.0
11-22 13:21:50.324: ERROR/(3110): LOOP ENTERED: 11 accelX to check for null: -1.553
11-22 13:21:50.434: DEBUG/rapi(3110): Successful request
11-22 13:21:50.434: INFO/global(3110): Default buffer size used in BufferedReader constructor. It would be better to be explicit if an 8k-char buffer is required.
11-22 13:21:50.444: INFO/(3110): 100 * (12/20)
11-22 13:21:50.444: INFO/(3110): 0 <- int cast original double -> 0.0
11-22 13:21:50.474: ERROR/(3110): LOOP ENTERED: 12 accelX to check for null: 3.895
11-22 13:21:50.584: DEBUG/dalvikvm(3110): GC_FOR_MALLOC freed 7642 objects / 590016 bytes in 95ms
11-22 13:21:50.684: DEBUG/rapi(3110): Successful request
11-22 13:21:50.684: INFO/global(3110): Default buffer size used in BufferedReader constructor. It would be better to be explicit if an 8k-char buffer is required.
11-22 13:21:50.694: INFO/(3110): 100 * (13/20)
11-22 13:21:50.694: INFO/(3110): 0 <- int cast original double -> 0.0
11-22 13:21:50.704: ERROR/(3110): LOOP ENTERED: 13 accelX to check for null: 3.895
11-22 13:21:50.814: DEBUG/rapi(3110): Successful request
11-22 13:21:50.814: INFO/global(3110): Default buffer size used in BufferedReader constructor. It would be better to be explicit if an 8k-char buffer is required.
11-22 13:21:50.824: INFO/(3110): 100 * (14/20)
11-22 13:21:50.824: INFO/(3110): 0 <- int cast original double -> 0.0
11-22 13:21:50.844: ERROR/(3110): LOOP ENTERED: 14 accelX to check for null: 6.320
11-22 13:21:51.033: DEBUG/rapi(3110): Successful request
11-22 13:21:51.033: INFO/global(3110): Default buffer size used in BufferedReader constructor. It would be better to be explicit if an 8k-char buffer is required.
11-22 13:21:51.033: INFO/(3110): 100 * (15/20)
11-22 13:21:51.033: INFO/(3110): 0 <- int cast original double -> 0.0
11-22 13:21:51.064: ERROR/(3110): LOOP ENTERED: 15 accelX to check for null: -3.868
11-22 13:21:51.164: DEBUG/rapi(3110): Successful request
11-22 13:21:51.164: INFO/global(3110): Default buffer size used in BufferedReader constructor. It would be better to be explicit if an 8k-char buffer is required.
11-22 13:21:51.164: INFO/(3110): 100 * (16/20)
11-22 13:21:51.164: INFO/(3110): 0 <- int cast original double -> 0.0
11-22 13:21:51.194: ERROR/(3110): LOOP ENTERED: 16 accelX to check for null: -9.820
11-22 13:21:51.304: DEBUG/rapi(3110): Successful request
11-22 13:21:51.304: INFO/global(3110): Default buffer size used in BufferedReader constructor. It would be better to be explicit if an 8k-char buffer is required.
11-22 13:21:51.314: INFO/(3110): 100 * (17/20)
11-22 13:21:51.314: INFO/(3110): 0 <- int cast original double -> 0.0
11-22 13:21:51.324: ERROR/(3110): LOOP ENTERED: 17 accelX to check for null: -5.530
11-22 13:21:51.454: DEBUG/rapi(3110): Successful request
11-22 13:21:51.454: INFO/global(3110): Default buffer size used in BufferedReader constructor. It would be better to be explicit if an 8k-char buffer is required.
11-22 13:21:51.464: INFO/(3110): 100 * (18/20)
11-22 13:21:51.464: INFO/(3110): 0 <- int cast original double -> 0.0
11-22 13:21:51.474: ERROR/(3110): LOOP ENTERED: 18 accelX to check for null: -2.601
11-22 13:21:51.583: DEBUG/rapi(3110): Successful request
11-22 13:21:51.583: INFO/global(3110): Default buffer size used in BufferedReader constructor. It would be better to be explicit if an 8k-char buffer is required.
11-22 13:21:51.583: INFO/(3110): 100 * (19/20)
11-22 13:21:51.594: INFO/(3110): 0 <- int cast original double -> 0.0
11-22 13:21:51.614: ERROR/(3110): LOOP ENTERED: 19 accelX to check for null: -6.020
11-22 13:21:52.043: DEBUG/dalvikvm(767): GC_EXPLICIT freed 347 objects / 19880 bytes in 129ms
11-22 13:21:52.054: DEBUG/rapi(3110): Successful request
11-22 13:21:52.054: INFO/global(3110): Default buffer size used in BufferedReader constructor. It would be better to be explicit if an 8k-char buffer is required.
11-22 13:21:52.054: INFO/(3110): 100 * (20/20)
11-22 13:21:52.054: INFO/(3110): 100 <- int cast original double -> 100.0
11-22 13:21:52.354: WARN/IInputConnectionWrapper(3110): finishComposingText on inactive InputConnection
你可以看到它说partialProg
总是0,我假设是因为它乘以一些十进制数&lt; 1到100.但是,它的类型是双!为什么会这样做?
我甚至尝试将它在许多地方进行类型转换为双倍 - 没有不同的结果。
关于这里发生了什么的任何想法?
答案 0 :(得分:1)
初始化partialProg
的方式无关紧要,因为您会立即覆盖它。
重要的是表达式100 * ((i+1) / len);
的类型,在您的情况下是整数(假设i
和{{1}是整数)。因此,表达式的值被约束为整数值。
要创建len
类型的表达式,只需将单个术语变为双精度。例如:
double
答案 1 :(得分:1)
你没有指定你的语言,所以我用Java测试了它。
当我将 len 变量定义为int
时,我看到了您的问题。当我将其切换为double
时,它会按预期工作。