samedi 20 juin 2015

Float comparision in C

#include<stdio.h>
int main()
{
    float x = 0.6;
    if (x == 0.6)
        printf("IF");
    else if (x == 0.6f)
        printf("ELSE IF");
    else
        printf("ELSE");
}

This code gives output ELSE IF

#include<stdio.h>
int main()
{
    float x = 0.5;
    if (x == 0.5)
        printf("IF");
    else if (x == 0.5f)
        printf("ELSE IF");
    else
        printf("ELSE");
}

This code gives output IF

Even though both the programs looks same but why there is difference in the outputs? Why this is happening?

Aucun commentaire:

Enregistrer un commentaire