Sample code (t0.c):
#include <stdio.h>
#include <limits.h>
#define F 2147483600.0f
int main(void)
{
printf("F %f\n", F);
printf("INT_MAX %d\n", INT_MAX);
printf("F <= INT_MAX %d\n", F <= INT_MAX);
if ( F <= INT_MAX )
{
printf("(int)F %d\n", (int)F);
}
return 0;
}
Invocations:
$ gcc t0.c && ./a.exe
F 2147483648.000000
INT_MAX 2147483647
F <= INT_MAX 1
(int)F 2147483647
$ clang t0.c && ./a.exe
F 2147483648.000000
INT_MAX 2147483647
F <= INT_MAX 1
(int)F 0
Questions:
F
is printed as 2147483648.000000
, then why F <= INT_MAX
is true?UPD. Solution:
if ( lrintf(F) <= INT_MAX )
{
printf("(int)F %d\n", (int)F);
}
UPD2. Better solution:
if ( F <= nextafter(((float)INT_MAX) + 1.0f, -INFINITY) )
{
printf("(int)F %d\n", (int)F);
}
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 islong 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 isdouble
.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 isfloat
.
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
.