int main()
{
float a = .7,b=0.7;
int c;
c = a <.7;
printf(“c=%d\n”,c);
}
output is 1,why?
and a=.5
c=a<.5
output is 0, why?
int main()
{
float a = .7,b=0.7;
int c;
c = a <.7;
printf(“c=%d\n”,c);
}
output is 1,why?
and a=.5
c=a<.5
output is 0, why?
It is because of precision issues for floating point numbers. That is why, most of the text books in C/C++ come with the warning “never compare two floating point numbers”.
Why it works for 0.5 is because, its binary representation is 0.1 (that is, it is a terminating decimal. So, all of its bits are well defined, at least within the precision limits of float). So, when numbers that can usually be represented by terminating binary representation are used, the results you get is the one you expect. Otherwise, the results are unpredictable.
ya i got… thanx
here 0.7 is double by default… and the size of double(8 bytes) is greater than sizeof float(4 bytes)
so compiler returns true… thats why the ouput is 1