float comparision in c. whats d output of below program
main()
{
float a = 0.7;
if(a==0.7)
printf("a");
else
printf("b");
}
if its b why so. how is the float comparision actually done
The default floating point type is double. Your comparison compares a float variable to a double constant, hence the inequality.
You can either add an 'f' after the constant (if a==0.7f) to make it a float or define a as a double (double a = 0.7), but the best way would be to use a custom comparison function that returns true if the delta between the two numbers is very small (the Knuth way). Read more here.
12
Got an answer for praveen? Would you like to comment on the posted answers, or vote for the one which you think is the best?
Sign up for a free account, or sign in (if you're already a member).
Other people asked questions on similar topics, check out the answers they received: