Can't figure out how come:
0.7 >0.7
is true.Seems to be a trick question.
void main()
float a=0.7;`
if(a < 0.7)`
{
printf("TRUE");
}
else
{
printf("FALSE");
}
}
This is because variable a
is a float
, and the constant 0.7
is double
. Representation of 0.7 in floating point types is not precise. When the value is converted to float
, the result becomes slightly smaller than the double
value. This difference could go both ways, i.e. float
representation may become slightly greater than the corresponding double
after the conversion.
Adding a cast will fix the problem:
if(a < (float)0.7) {
printf("TRUE");
} else {
printf("FALSE");
}