Confused with if else

This code prints "What is this". But It seems to me the result should be "lol"
Why is that ?? Plz help .
#include<stdio.h>
int main()
{
float a = 0.7;
if(a < 0.70)
printf("what is this?");
else
printf("lol");
return 0;
}
This is a precision problem of floating point types like float and double. 0.7 can't be stored exactly. When you write 0.7 or 0.70 the type is a double. The extra zero doesn't make a difference. float usually have less precision than double so when you assign 0.7 (a double) to a (a float) it will store 0.7 less exact, and apparently as a smaller value. If you change the type of a to double you will get the expected output.
Oh
Now I get it . Crystal clear .
Thanks a lot Mr. Peter. :) :)
Oh another question arise !
When I use .5 or 1.5 or 2.5 instead of .7 it give me the correct out put .
I mean "lol" not "What is this"
Why is that ??
@ Mr. Peter
Because 1.5 and 2.5 can be stored exactly without any rounding errors.
Topic archived. No new replies allowed.