...
Consequently, in implementations that do not allow for the representation of all numbers, conversions of values between zero and FLT_MIN
may result in undefined behavior. For example, if an implementation supports denormal numbers the conversion is representable, if not, the behavior is undefined.
This rule does not apply to demotions of floating-point types on implementations that support signed infinity, such as IEEE 754, as all values are within range.
...
This compliant solution assumes that the range of values of type float
is greater than that of an int
, as is the case in most implementations. Unfortunately, there is no safe way to inquire about this assumption in the code short of already knowing the implementation. Converting INT_MAX
to float
is a problem on many implementations, resulting in a power of 2, rather than 1 less. number one greater than the value of INT_MAX. Converting INT_MIN
to float
is a problem on many implementations, resulting in a number one less than the value of INT_MIN.
Code Block | ||||
---|---|---|---|---|
| ||||
#include <limits.h> void func(float f_a) { int i_a; if (f_a >= ((float)INT_MAX -1.0) || f_a < ((float)INT_MIN +1.0)|| (f_a >= 0F && f_a < FLT_MIN)) { /* Handle error */ } else { i_a = f_a; } } |
...