The output of the following program is block 2 . But why? Can you explain this?
- EddieJLv 79 months ago
Try running this:
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");