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.
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.
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.
Casting the int to a float explicitly will do absolutely nothing. The int will be promoted to a float for purposes of comparison anyway.
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.
Is it always possible to convert an
int
to afloat
?
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With