更新时间:2023-02-17 20:52:10
此答案假定 float
是编码为 32 位的 IEEE-754 单精度浮点数,并且 int
是 32 位.有关 IEEE-754 的更多信息,请参阅这篇***文章.
This answer assumes that float
is an IEEE-754 single precision float encoded as 32-bits, and that an int
is 32-bits. See this Wikipedia article for more information about IEEE-754.
浮点数只有 24 位精度,而 int 则为 32 位.因此,从 0 到 16777215 的 int 值具有作为浮点数的精确表示,但大于 16777215 的数字不一定具有作为浮点数的精确表示.以下代码演示了这一事实(在使用 IEEE-754 的系统上).
Floating point numbers only have 24-bits of precision, compared with 32-bits for an int. Therefore int values from 0 to 16777215 have an exact representation as floating point numbers, but numbers greater than 16777215 do not necessarily have exact representations as floats. The following code demonstrates this fact (on systems that use IEEE-754).
for ( int a = 16777210; a < 16777224; a++ )
{
float b = a;
int c = b;
printf( "a=%d c=%d b=0x%08x\n", a, c, *((int*)&b) );
}
预期输出是
a=16777210 c=16777210 b=0x4b7ffffa
a=16777211 c=16777211 b=0x4b7ffffb
a=16777212 c=16777212 b=0x4b7ffffc
a=16777213 c=16777213 b=0x4b7ffffd
a=16777214 c=16777214 b=0x4b7ffffe
a=16777215 c=16777215 b=0x4b7fffff
a=16777216 c=16777216 b=0x4b800000
a=16777217 c=16777216 b=0x4b800000
a=16777218 c=16777218 b=0x4b800001
a=16777219 c=16777220 b=0x4b800002
a=16777220 c=16777220 b=0x4b800002
a=16777221 c=16777220 b=0x4b800002
a=16777222 c=16777222 b=0x4b800003
a=16777223 c=16777224 b=0x4b800004
这里有趣的是 float
值 0x4b800002 用于表示三个 int
值 16777219、16777220 和 16777221,从而将 16777219 转换为 float
并返回到 int
不会保留 int
的确切值.
Of interest here is that the float
value 0x4b800002 is used to represent the three int
values 16777219, 16777220, and 16777221, and thus converting 16777219 to a float
and back to an int
does not preserve the exact value of the int
.
最接近INT_MAX
的两个浮点值是2147483520和2147483648,可以用这段代码演示
The two floating point values that are closest to INT_MAX
are 2147483520 and 2147483648, which can be demonstrated with this code
for ( int a = 2147483520; a < 2147483647; a++ )
{
float b = a;
int c = b;
printf( "a=%d c=%d b=0x%08x\n", a, c, *((int*)&b) );
}
输出中有趣的部分是
a=2147483520 c=2147483520 b=0x4effffff
a=2147483521 c=2147483520 b=0x4effffff
...
a=2147483582 c=2147483520 b=0x4effffff
a=2147483583 c=2147483520 b=0x4effffff
a=2147483584 c=-2147483648 b=0x4f000000
a=2147483585 c=-2147483648 b=0x4f000000
...
a=2147483645 c=-2147483648 b=0x4f000000
a=2147483646 c=-2147483648 b=0x4f000000
请注意,从 2147483584 到 2147483647 的所有 32 位 int
值将向上舍入为 float
值 2147483648.最大的 int
向下舍入的值为 2147483583,与 32 位系统上的 (INT_MAX - 64)
相同.
Note that all 32-bit int
values from 2147483584 to 2147483647 will be rounded up to a float
value of 2147483648. The largest int
value that will round down is 2147483583, which the same as (INT_MAX - 64)
on a 32-bit system.
因此,人们可能会得出结论,(INT_MAX - 64)
以下的数字将安全地从 int
转换为 float
并返回到 int代码>.但这仅适用于
int
的大小为 32 位且 float
根据 IEEE-754 编码的系统.
One might conclude therefore that numbers below (INT_MAX - 64)
will safely convert from int
to float
and back to int
. But that is only true on systems where the size of an int
is 32-bits, and a float
is encoded per IEEE-754.