I have a simple C snippet as follows:
#include<stdio.h>
#include<conio.h>
int main()
{
float a=0.3,b=0.5
clrscr();
if(a==0.3)
{
printf("equal");
}
else
{
printf("unequal");
}
if(b==0.5)
{
printf("equal");
}
else
{
printf("unequal");
}
getch();
}
shows output as: unequal equal.
I understand this because computer takes 0.3 as 1/3 and as 0.333... is not equal to 0.33, it shows output unequal, while in 0.5 is precise number so it gives output as equal.
But now if I take float b=0.2 like this:
float b=0.2;
if(b==0.2)
{
printf("equal");
}
else
{
printf("unequal");
}
Then it also shows unequal as output! As 0.2 is 1/5 - precise value, it should give output equal.
Can anyone tell me if anything is wrong here?