C program - Why does this happen??
Please explain this strange behavior to me. It's a simple program.
#include<stdio.h>
main()
{
float a=1.5;
if(a==1.5) printf("Equal");
else printf("Not Equal");
}
Output I got was "Equal".Now,
#include<stdio.h>
main()
{
float a=1.2;
if(a==1.2) printf("Equal");
else printf("Not Equal");
}
Now, Output was "Not Equal".Why does this happen? ie, in float data type, when you compare values like 1,1.5,2,2.5 etc, result is correct.
If you compare 1.2,1.7,2.4 etc, result is not correct. Why is this??
PS - Strangely, if you use double, output is correct for all cases. I use Turbo C 16 bit compiler. Any idea??