+3

Why this code printing "hi" ?

float a = 0.7; if(0.7 > a) printf("hi"); else printf("hello");

2/25/2020 7:46:02 AM

Ravi Coder

4 Answers

New Answer

+4

computer are not able to compute exactly. Better to ask for a difference: if 0.7-a > 0.0001

+3

And also, default type for decimal number is double type so 0.7 is here is decimal type.. And you are comparing 0.7 of double higher type with 'a' float variable value of 0.7 which is less.. 0.7 of double, if converted to float 0.7f is slightly less value than 0.7 double in internal representations..

+1

The magic of floating point numbers and the issues that arise when binary code has to deal with decimal numbers.

+1

doube i = 0.7; if(0.7 > i) cout << "hi"; else cout << "hello"; OUTPUT: hello (in c++)