Suppose I
is some integer type and F
some (real) floating point type.
I want to write two functions. The first function shall take a value i
of type I
and return a boolean indicating whether i
converted to F
falls into the representable range, i.e. whether (F)i
will have defined behavior.
The second function shall take a value f
of type F
and return a boolean indicating whether f
converted to I
falls into the representable range, i.e. whether (I)f
will have defined behavior.
Is it possible to write such a function that will be, on every implementation conforming to the standard, correct and not exhibit undefined behavior for any input? In particular I do not want to assume that the floating point types are IEEE 754 types.
I am asking about both C and C++ and their respective standard versions separately, in case that changes the answer.
Basically the intention of this question is to figure out whether (sensible) floating-point / integral conversions are possible without relying on IEEE 754 or other standards or hardware details at all. I ask out of curiosity.
Comparing against e.g. INT_MAX
or FLT_MAX
does not seem to be possible, because it is not clear which type to do the comparison in without already knowing which of the types has wider range.