+ 1

#include <stdio.h> int main() { float f1=0.1; if(f1==0.1) printf ("hello"); else printf ("hi"); }

Why the condition is false and hi is printed?

22nd Jun 2019, 5:16 PM
Rohit Ahuja
Rohit Ahuja - avatar
5 Réponses
+ 4
It's because floating point numbers can't really be represented in binary. If you understand bases, you could write 0.5 in binary like this: 0.1 1 * 2^-1 = 0.5. It is really difficult to represent 0.1, because you can describe numbers like this: 1/2, 1/4, 1/8, 1/16, 1/32...
22nd Jun 2019, 6:05 PM
Airree
Airree - avatar
+ 2
Thanx to all
23rd Jun 2019, 4:21 PM
Rohit Ahuja
Rohit Ahuja - avatar
+ 2
Note, you can also test against a range of 0.0999999 and 0.1000001
24th Jun 2019, 12:17 AM
John Wells
John Wells - avatar
+ 1
Thanks John well
1st Jul 2019, 12:36 PM
Rohit Ahuja
Rohit Ahuja - avatar