0
int main() 
{ 
   float f=0.5; 
   if(f==0.5) 
       printf("True\n"); 
   else 
       printf("False\n"); 
} 

The answer is True in this case. If i take f value as 0.1 or 0.2 or 0.3 etc, the answer is False. Can anyone explain the reason for this.

John Kugelman
  • 349,597
  • 67
  • 533
  • 578
  • because `0.5` (or `0.1`, `0.2`, ...) is a double and `f` is a float. double can be, for instance, `0.10000000001111111` whereas the "same" float can be `0.100000000011` because it has less precision (converted to double `0.10000000001100000`). – pmg Jun 24 '21 at 15:25
  • 1
    ... and `0.5` is a power of 2, and floats are stored in binary form. – Adrian Mole Jun 24 '21 at 15:26
  • ... try with `0.25` and `0.125`, just to see what happens. – Adrian Mole Jun 24 '21 at 15:31
  • Thanks Adrian. But even 0.1 and 0.2,... are also stored in binary. Then what makes it different from 0.5 – ENUGALA RAJU Jun 28 '21 at 09:53

0 Answers0