Possible Duplicate:
strange output in comparision of float with float literal
Comparison of float and double variables
I have a test with double and float in C, but I cannot explain why.
float x = 3.4F;
if(x==3.4)
printf("true\n");
else printf("false\n");
double y = 3.4;
if (y==3.4)
printf("true\n");
else printf("false\n");
The result will be False and True. Please explain for me please.
No guarantee that the result will be false and true, but the basic idea is pretty simple:
3.4has type double. When you assign it to afloat, it’ll get rounded. When you compare, that rounded number will be promoted back to a double, not necessarily the same double as3.4.In the second case, everything’s double throughout.