1.0乘法precision和int浮动转换 [英] Precision of multiplication by 1.0 and int to float conversion
问题描述
它是安全的假设条件(INT)(我* 1.0F)== I
适用于任何整数 I
?
Is it safe to assume that the condition (int)(i * 1.0f) == i
is true for any integer i
?
推荐答案
没有。
如果 I
足够大, INT(浮点(I))!= I
(假设浮动是IEEE -754单precision, I = 0x1000001
足以显示出这一点),那么这是假的,因为乘以 1.0F
强制转换为浮动
,它改变了价值,即使随后的乘法没有。
If i
is sufficiently large that int(float(i)) != i
(assuming float is IEEE-754 single precision, i = 0x1000001
suffices to exhibit this) then this is false, because multiplication by 1.0f
forces a conversion to float
, which changes the value even though the subsequent multiplication does not.
但是,如果 I
是一个32位整数和双击
是IEEE-754双,那么它是的事实, INT(我* 1.0)== I
。
However, if i
is a 32-bit integer and double
is IEEE-754 double, then it is true that int(i*1.0) == i
.
只是要完全清楚,由 1.0F
乘法的是的准确。这是一个从 INT
到浮动转换
这可能不是。
Just to be totally clear, multiplication by 1.0f
is exact. It's the conversion from int
to float
that may not be.
这篇关于1.0乘法precision和int浮动转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!