Let's say I have two objects i
and f
of respective types I
and F
. I know that std::is_integral<I>::value
is true, and std::is_floating_point<F>::value
is true.
Is there a fully standards-compliant way to find out if the value of i
is smaller than the value of f
? Note the emphasis on 'fully standards-compliant', for this question I'm only interested in answers that are backed up by guarantees from the C++ standard.
The trivial implementation i < I(f)
doesn't work, because the value of f
may not fit inside i
. The trivial implementation F(i) < f
doesn't work either, because the precision of f
may not be enough to represent i
, causing i
to get rounded to a value equal to f
(if you have IEEE754 floats, 16777219 < 16777220.f
fails).
But here comes the real dilemma: if you want to use std::numeric_limits::max
to alleviate these problems, your back to the original problem of comparing floats and integers! This is because the type of std::numeric_limits::max
is equal to the original type.