about datatype

main()
{float a=3.7;
if (a==3.7)
cout<<“true”;
else
cout<<“false”;
}

my output is false ,why ?
give any solid explaination for it.

There are some things you should know before using float:

  1. Single precision, called “float” in the C language family, and “real” or “real*4” in Fortran. This is a binary format that occupies 32 bits (4 bytes) and its significand has a precision of 24 bits (about 7 decimal digits). (source wikipedia)

  2. printf("%.10f",a); gives 3.7000000477

So, float value is accurate upto 7th decimal point digit. That’s why this condition fails “if (a==3.7)”.

P.S.: For better precision use double.

3 Likes

main()

{
float a=3.7;

if ( a ==(float) 3.7)

cout<<“true”;

else

cout<<“false”;

}

@sobhagya : if by (source wiki) you are referring to wikipedia page, then i would like to point out that ‘wiki’ is a different thing. Wiki is usually a web application, which allows people to add, modify, or delete content in a collaboration with others, whereas the encyclopedia project Wikipedia is the most famous wiki on the public web(from wikipedia).

1 Like

@codeanindya Thanks for that wonderful Information.

@dev233kmr if you like any answer then you can simply like it or award points. But, if you don’t want to loose your karma points then simply like it. And, if that answer is satisfying then simply mark it as correct answer.

//