1

How If() function actually works. I got totally confused when I run these three program.Could any one explain me how these outputs are coming?

Program 1:

int main(void)
{
  float a=4.2;
  clrscr();
  if(a==4.2)
    printf("Equal");
  else if(a<4.2)
    printf("Less");
  else if(a>4.2)
    printf("Greater");
  getch();
}

Output: Less

Program 2:

int main(void)
{
  float a=3.2;
  clrscr();
  if(a==3.2)
    printf("Equal");
  else if(a<3.2)
    printf("Less");
  else if(a>3.2)
    printf("Greater");
  getch();
}

Output: Greater

Program 3:

int main(void)
{
  float a=3.5;
  clrscr();
  if(a==3.5)
    printf("Equal");
  else if(a<3.5)
    printf("Less");
  else if(a>3.5)
    printf("Greater");
  getch();
}

Output: Equal

  • 3
    Please read ["What Every Computer Scientist Should Know About Floating-Point Arithmetic"](http://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.html). – Some programmer dude Apr 05 '13 at 13:20
  • this also http://stackoverflow.com/questions/3962724/problems-in-floating-point-comparison – MOHAMED Apr 05 '13 at 13:24
  • Did you even look? https://www.google.com/search?q=floating+point+comparison&oq=floating+point+comparison&aqs=chrome..69i57j5j69i65j0l2.3157j0&sourceid=chrome&ie=UTF-8 – Ed S. Sep 22 '13 at 20:40

0 Answers0