You're comparing a value of type int
with a value of type float
. The operands of the <=
operator need to first be converted to a common type to evaluate the comparison.
This falls under the usual arithmetic conversions. In this case, the value of type int
is converted to type float
. And because the value in question (2147483647) cannot be represented exactly as a float
, it results in the closest representable value, in this case 2147483648. This matches what the constant represented by the macro F
converts to, so the comparison is true.
Regarding the cast of F
to type int
, because the integer part of F
is outside the range of an int
, this triggers undefined behavior.
Section 6.3.1.4 of the C standard dictates how these conversions from integer to floating point and from floating point to integer are performed:
1 When a finite value of real floating type is converted to an integer type other than _Bool
, the fractional part is discarded
(i.e., the value is truncated toward zero). If the value of
the integral part cannot be represented by the integer type, the
behavior is undefined.
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. Results of some implicit conversions may
be represented in greater range and precision than that
required by the new type (see 6.3.1.8 and 6.8.6.4)
And section 6.3.1.8p1 dictates how the usual arithmetic conversions are performed:
First, if the corresponding real type of either operand is long double
, the other operand is converted, without change of type domain,
to a type whose corresponding real type is long double
.
Otherwise, if the corresponding real type of either operand
is double
, the other operand is converted, without change of type
domain, to a type whose corresponding real type is double
.
Otherwise, if the corresponding real type of either operand
is float
, the other operand is converted, without change of type
domain, to a type whose corresponding real type is float
.
As for how to avoid undefined behavior in this case, if the constant F
has no suffix i.e. 2147483600.0
then it has type double
. This type can represent exactly any 32 bit integer value, so the given value is not rounded and can be stored in an int
.