将double转换为int的结果是错误的 [英] Result of casting double to int is wrong
问题描述
当我运行下面的代码似乎有一些晦涩的舍入错误:
int roundedTotal = (PriorityJob * 100.0);
最初 PriorityJob = 1.4
和 roundedTotal
未定义。评估 PriorityJob * 100.0
此时给出 140
。之后 roundedTotal = 139
。
显然,140.0被解释为139.99999。这是浮点引擎的缺陷吗?
正如 1/3 = 0.33333333。
1/10
(因此大多数非整数十进制值,包括 1.4
)不能精确表示为二进制小数。 您可能希望四舍五入为最接近的整数,而代之以最接近的可表示值,它可能稍微大于或小于true :(int)(PriorityJob * 100.0 + 0.5)
There seems to be some kind of obscure rounding error when I run the following code:
int roundedTotal = (int)(PriorityJob * 100.0);
Initially PriorityJob = 1.4
and roundedTotal
is undefined. Evaluating PriorityJob * 100.0
at that point gives 140
. Afterwards roundedTotal = 139
.
Apparently, 140.0 is being interpreted as 139.99999. Is this a deficiency in the floating point engine? I have never seen anything like it.
Just about every modern computer uses a binary representation for floating-point numbers.
Just as 1/3 = 0.33333333...
can't be represented exactly as a decimal fraction, so 1/10
(and hence most non-integer decimal values, including 1.4
) can't be represented exactly as a binary fraction. It will instead be represented by the nearest representable value, which may be slightly more or less than the "true" value.
You might want to round to the nearest integer instead: (int)(PriorityJob * 100.0 + 0.5)
这篇关于将double转换为int的结果是错误的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!