0.1浮大于0.1双大。我希望它是假的 [英] 0.1 float is greater than 0.1 double. I expected it to be false
问题描述
让:
double d = 0.1;
float f = 0.1;
宜恩pression
should the expression
(f > d)
收益真正
或假
?
根据经验,答案是真正
。不过,我希望它是假
。
Empirically, the answer is true
. However, I expected it to be false
.
由于 0.1
不能在二进制psented完全重新$ P $,而双有 15
到 16
小数precision的数字,浮动只有 7
。于是,他们俩都小于 0.1
,而双更接近 0.1
。
As 0.1
cannot be perfectly represented in binary, while double has 15
to 16
decimal digits of precision, and float has only 7
. So, they both are less than 0.1
, while the double is more close to 0.1
.
我需要一个确切的解释真正
。
I need an exact explanation for the true
.
推荐答案
我想说转换双击
到<$ C $时,答案取决于舍入模式C>浮动。 浮动
已24的二进制的precision和位双
有53二,0.1是:
I'd say the answer depends on the rounding mode when converting the double
to float
. float
has 24 binary bits of precision, and double
has 53. In binary, 0.1 is:
0.1₁₀ = 0.0001100110011001100110011001100110011001100110011…₂
^ ^ ^ ^
1 10 20 24
因此,如果我们圆的最多的在第24位,我们会得到
So if we round up at the 24th digit, we'll get
0.1₁₀ ~ 0.000110011001100110011001101
比的精确值,在53个数字越precise近似更大
which is greater than the exact value and the more precise approximation at 53 digits.
这篇关于0.1浮大于0.1双大。我希望它是假的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!