Float and Double value creating confusion in c [du

2019-09-09 17:21发布

问题:

This question already has an answer here:

  • What is the difference between float and double? 11 answers
  • Floating point comparison [duplicate] 5 answers

I am running this program. But getting strange result.

int  main()
{
float a=.8;
if(a < .8)
    printf("a is small\n");
else
    printf("a is not small\n");

float b=.7;
if(b < .7)
    printf("b is small\n");
else
    printf("b is not small\n");
return 0;
}

It gives me output--

a is not small

b is small

回答1:

Why don't you use 0.7f. 0.7 Is by default double, so we cannot predict it's behaviour