windows 从无符号整数到浮点数的转换
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3401722/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Conversion from unsigned int to float
提问by T.T.T.
warning C4244: '=' : conversion from 'unsigned int' to 'float', possible loss of data
Shouldn't a float be able to handle any value from an int?
浮点数不应该能够处理来自 int 的任何值吗?
unsigned int: 0 to 4,294,967,295
float 3.4E +/- 38 (7 digits)
Wiki:
维基:
The advantage of floating-point representation over fixed-point (and
integer) representation is that it can support a much wider range of values.
The advantage of floating-point representation over fixed-point (and
整数) 表示是它可以支持更广泛的值。
Any insight would be helpful, thanks.
任何见解都会有所帮助,谢谢。
http://msdn.microsoft.com/en-us/library/s3f49ktz%28VS.80%29.aspx
http://msdn.microsoft.com/en-us/library/s3f49ktz%28VS.80%29.aspx
回答by JaredReisinger
'unsigned int' and 'float' both use 32 bits to store values. Since a float has a larger range, it necessarily sacrifices some precision. This means that there are some unsigned int values that cannotbe accurately represented in a float. MSDNprovides some more details.
'unsigned int' 和 'float' 都使用 32 位来存储值。由于浮点数具有更大的范围,因此必然会牺牲一些精度。这意味着有些 unsigned int 值无法用浮点数准确表示。MSDN提供了更多详细信息。
回答by Dour High Arch
While float
supports a wider range of values than unsigned int
, it does so with less accuracy. Floats have a 23-bit mantissa which, as you posted, is only about 7 decimal digits. unsigned ints support over 9 decimal digits.
虽然float
支持比 更广泛的值unsigned int
,但它的准确性较低。浮点数有一个 23 位尾数,正如您所发布的,它只有大约 7 位十进制数字。无符号整数支持超过 9 位十进制数字。
Suggestion: use double
, not float
.
建议:使用double
,而不是float
。
Edit:Actually, I retract that suggestion; floating-point and integer data types are fundamentally different and are not directly convertible. What integer value do you expect to get for Single.MaxValue
? For Single.NegativeInfinity
? Explaining why you want to convert from float
to int
would be helpful.
编辑:实际上,我撤回了那个建议;浮点和整数数据类型根本不同,不能直接转换。你期望得到什么整数值Single.MaxValue
?对于Single.NegativeInfinity
? 解释为什么要从 转换float
为int
会很有帮助。
回答by You
回答by sje397
From wikipedia:
来自维基百科:
Single precision, called "float" in the C language family, and "real" or "real*4" in ? Fortran. This is a binary format that occupies 32 bits (4 bytes) and its significand has a precision of 24 bits (about 7 decimal digits).
单精度,在 C 语言家族中称为“float”,在 ? 复式。这是一种二进制格式,占用 32 位(4 个字节),其有效位精度为 24 位(约 7 个十进制数字)。
With an int having 32 bits (generally) and a float having 32 bits (with part reserved for the exponent), there are a lot of int values that can't be exactly represented by the floating point type.
具有 32 位(通常)的 int 和具有 32 位的浮点数(部分保留为指数),有很多 int 值不能由浮点类型精确表示。
回答by franji1
try this to understand the situation
试试这个来了解情况
float f = 0.0f;
while (f < (INT_MAX-1))
f++;
and you'll see that this is really an infinite loop on systems where int is 32 bits or less.
你会发现这在 int 为 32 位或更少的系统上确实是一个无限循环。
What will be interesting is to break into the infinite loop and see what the value is for f such that f == f + 1
有趣的是进入无限循环并查看 f 的值是什么,使得 f == f + 1