开发者

Conversion from unsigned int to float

开发者 https://www.devze.com 2023-01-10 06:53 出处:网络
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?
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?

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.

Any insight would be helpful, than开发者_开发知识库ks.

http://msdn.microsoft.com/en-us/library/s3f49ktz%28VS.80%29.aspx


'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 cannot be accurately represented in a float. MSDN provides some more details.


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.

Suggestion: use double, not 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.


As you note in your question, the float only takes 7 digits, as opposed to the 10 digits in INT_MAX. I believe this would be the reason to give a C4244 warning at this conversion.


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).

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.


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.

What will be interesting is to break into the infinite loop and see what the value is for f such that f == f + 1

0

精彩评论

暂无评论...
验证码 换一张
取 消