Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it always possible to convert an `int` to a `float`

Is a conversion from an int to a float always possible in C without the float becoming one of the special values like +Inf or -Inf?

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

I think a 128 bit int would cause an issue for a platform with an IEEE754 float as that has an upper value of around the 127th power of 2.

like image 716
Willy Wonka Avatar asked Jun 30 '17 12:06

Willy Wonka


People also ask

Can int be converted to float?

Integer and Float Conversions Integers and floats are data types that deal with numbers. To convert the integer to float, use the float() function in Python. Similarly, if you want to convert a float to an integer, you can use the int() function.

Can int be converted to float in C?

No, because you do the expression using integers, so you divide the integer 50 by the integer 100, which results in the integer 0. Type cast one of them to a float and it should work.

Can integer value be equal to float value?

Casting the int to a float explicitly will do absolutely nothing. The int will be promoted to a float for purposes of comparison anyway.


2 Answers

Short answer to your question: no, it is not always possible.

But it is worthwhile to go a little bit more into details. The following paragraph shows what the standard says about integer to floating-point conversions (online C11 standard draft):

6.3.1.4 Real floating and integer

2) When a value of integer type is converted to a real floating type, if the value being converted can be represented exactly in the new type, it is unchanged. If the value being converted is in the range of values that can be represented but cannot be represented exactly, the result is either the nearest higher or nearest lower representable value, chosen in an implementation-defined manner. If the value being converted is outside the range of values that can be represented, the behavior is undefined. ...

So many integer values may be converted exactly. Some integer values may lose precision, yet a conversion is at least possible. For some values, however, the behaviour might be undefined (if, for example, an integer value would not be able to be represented with the maximum exponent of the float value). But actually I cannot assume a case where this will happen.

like image 66
Stephan Lechner Avatar answered Nov 15 '22 13:11

Stephan Lechner


Is it always possible to convert an int to a float?

Reasonably - yes. An int will always convert to a finite float. The conversion may lose some precision for great int values.

Yet for the pedantic, an odd compiler could have trouble.


C allows for excessively wide int, not just 16, 32 or 64 bit ones and float could have a limit range, as small as 1e37.

It is not the upper range of int or INT_MAX that should be of concern. It is the lower end. INT_MIN which often has +1 greater magnitude than INT_MAX.

A 124 bit int min value could be about -1.06e37, so that does exceed the minimal float range.

With the common binary32 float, an int wound need to be more than 128 bits to cause a float infinity.


So what test is needed to detect this rare situation?

Form an exact power-of-2 limit and perform careful math to avoid overflow or imprecision.

#if -INT_MAX == INT_MIN
  // rare non 2's complement machine
  #define INT_MAX_P1_HALF (INT_MAX/2 + 1)
  _Static_assert(FLT_MAX/2 >= INT_MAX_P1_HALF, "non-2's comp.`int` range exceeds `float`");
#else
  _Static_assert(-FLT_MAX <= INT_MIN, "2's complement `int` range exceeds `float`");
#endif
like image 42
chux - Reinstate Monica Avatar answered Nov 15 '22 11:11

chux - Reinstate Monica