0

How this output has came?

#include <stdio.h> int main() { float a; a = 0.7; if(0.7>a) { printf("Hi"); }else{ printf("Hello"); } return 0; ----------------------------------------- OUTPUT Hi

4th Apr 2022, 3:34 AM
Ritwick Raj Makhal
Ritwick Raj Makhal - avatar
2 Answers
+ 5
G'day Ritwick Raj Makhal it is a binary to decimal rounding error. Add this to your code and see what the value of a becomes: printf("%.16f",a); https://code.sololearn.com/c8Joyu2AI8yi/?ref=app
4th Apr 2022, 4:37 AM
HungryTradie
HungryTradie - avatar
+ 4
It happens because the comparison took two floating point values of different precision as operands. The 0.7 literal is a `double` while <a> was a `float`. The difference may be noticeable when printing the two values with enough decimal points specified, as demonstrated by HungryTradie above ... You can compare <a> with a `float` literal (rather than `double` literal) by appending 'f' to the literal, which specifies the literal to be a `float` rather than a `double`. if( 0.7f > a ) // <- notice the 'f' suffix But this far I have learned that floating point comparison just can't be as solid as integer comparison : )
4th Apr 2022, 5:51 AM
Ipang