You can always convert `int` to` float`

Is conversion from int to float always possible in C without float , becoming one of special values โ€‹โ€‹like + Inf or -Inf?

AFAIK there is no upper limit in the range of int .

I think that 128 bits of int will cause a problem for the platform with IEEE754 float , since it has an upper value around 127th power 2.

+5
source share
3 answers

Is it always possible to convert int to float ?

Reasonable - yes. int will always be converted to the final float . The conversion may lose some precision for large int values.

However, for pedantry the odd compiler may have problems.


C allows an excessively wide int , not just 16, 32, or 64 bits, and a float can have a limit range of 137.

This is not the top range of int or INT_MAX , which should be troubling. This is the bottom end. INT_MIN , which often has a +1 larger value than INT_MAX .

A value of 124 bits int min can be around -1.06e37, so it exceeds the minimum float range.

With a common binary32 float int wound must be greater than 128 bits in order to cause an infinity float .


So, what test is needed to detect this rare situation?

Form an exact power limit of-2 and do thorough math to avoid overflow or inaccuracy.

 #if -INT_MAX == INT_MIN // rare non 2 complement machine #define INT_MAX_P1_HALF (INT_MAX/2 + 1) _Static_assert(FLT_MAX/2 >= INT_MAX_P1_HALF, "non-2 comp.`int` range exceeds `float`"); #else _Static_assert(-FLT_MAX <= INT_MIN, "2 complement `int` range exceeds `float`"); #endif 
+1
source

A short answer to your question: no, this is not always possible.

But itโ€™s worth a little more detail on the details. The following paragraph shows that the standard talks about integer-to-floating conversions (an online draft of the C11 standard ):

6.3.1.4 Real floating and integer numbers

2) When the value of an integer type is converted to a real floating type, if the converted value can be represented exactly in the new type, it has not changed. If the converted value is in the range of values โ€‹โ€‹that can be represented but cannot be represented exactly, the result is either the closest higher or closest lower representable value selected in accordance with the implementation. If the value is converted outside the range of values โ€‹โ€‹that can be represented, the behavior is undefined ....

So many integer values โ€‹โ€‹can be converted exactly. Some integer values โ€‹โ€‹may lose precision, but conversion is at least possible. However, for some values, the behavior may be undefined (if, for example, an integer value cannot be represented with a maximum exponent of the float value). But in fact, I canโ€™t imagine the case when this happens.

+8
source

The standard requires floating point representations to include a finite number equal to 10 37 ( ? 5.2.4.2. 2/12 ) and does not limit the maximum size of an integer. So if your implementation has 128-bit integers (or even 124-bit integers), you can convert the integer value to float to exceed the range of finite representable floating point numbers.

+1
source

Source: https://habr.com/ru/post/1269368/


All Articles