+ 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?
5 ответов
+ 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...
+ 2
Thanx to all
+ 2
Note, you can also test against a range of 0.0999999 and 0.1000001
+ 1
Thanks John well