promotion image of download ymail app

The output of the following program is block 2 . But why? Can you explain this?



void main()


float a=1.3;

if (a==1.3)

printf ("block1");


printf ("block2");


1 Answer

  • EddieJ
    Lv 7
    9 months ago

    Try running this:

    float a=1.3;

    double b = 1.3;

    printf ("%30.25f\n", a);

    printf ("%30.25f\n", b);

    In your if statement, the literal 1.3 gets turned into a double, so you are comparing a double to a float. The conversion into a binary representation isn't perfect.

    You could test the absolute value of the difference.

    if (fabs (a-b) < 0.000001) printf ("close enough\n");

    if (fabs (a-1.3) < 0.000001) printf ("close enough\n");

    • Commenter avatarLog in to reply to the answers
Still have questions? Get answers by asking now.