+ 3

Why this code printing "hi" ?

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

25th Feb 2020, 7:46 AM
Ravi Coder
Ravi Coder - avatar
4 Respostas
+ 4
computer are not able to compute exactly. Better to ask for a difference: if 0.7-a > 0.0001
25th Feb 2020, 7:50 AM
Oma Falk
Oma Falk - avatar
+ 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..
25th Feb 2020, 8:06 AM
Jayakrishna 🇮🇳
+ 1
The magic of floating point numbers and the issues that arise when binary code has to deal with decimal numbers.
25th Feb 2020, 10:40 AM
Fernando Pozzetti
Fernando Pozzetti - avatar
+ 1
doube i = 0.7; if(0.7 > i) cout << "hi"; else cout << "hello"; OUTPUT: hello (in c++)
27th Feb 2020, 7:43 AM
Eyob
Eyob - avatar