Can anyone give explanation for output of this simple code?

why the output of this code is coming “B”? shouldn’t it come “C”? any possible explanation?

#include <cstdio>
int main()
{
float t = 0.7;
float q = 0.5;
if(t < 0.7)
{
if(q < 0.5) printf("A");
else printf("B");
}
else 
printf("C");
return 0;
}

Let me start with floating point comparisons are dangerous!!

a float variable can store only upto 5 digits after the decimal point. The reason float comparison works for 0.5 is beacuse 0.5 is easily expressed as a power of 2 in binary (2 ^ (-1)) while 0.7 is not easily expressed so errors come up when storing the number in binary form.Hence t< 0.7 is True but q<0.5 return False. Hope this makes it clear. would like to thank @tijoforyou for his explanation on this which helped me understand.

thanx… it made it clear…

Pls accept the answer and close this qn

Due to loss of precision. Similar question here http://stackoverflow.com/questions/18377240/understanding-float-variable-comparison-in-if

And the doc, that I recommend to everyone asking these kinda qeustions: http://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.html