+ 4
#include <stdio.h> int main() { float a=0.7; if(a>0.7) printf("condition true"); else printf ("condition false");}
Case 1.. if(a>0.7) it will print Condition false.. Case 2.. if(a<0.7) it will print Condition true.. how?
2 Réponses
+ 4
Run this and you'll have an idea why.
Float data type has precision upto 7 digits while double data has precision upto 14 digits.
Float loses precision after 7 digits. So double has more precision.
Since variable 'a' is of float data type while 0.7 in the if condition is of double data type. 'a' loses its precision which results in false boolean value.
#include <stdio.h>
int main() {
float a = 0.7;
if(a > 0.7)
printf("condition true\n");
else
printf ("condition false\n");
printf("float: %.14f\n", a);
printf("double: %.14f\n", 0.7);
}
+ 4
thankx