+ 1

Why this is false?

#include <iostream> using namespace std; int main() { float f = 0.1; if (f == 0.1) { cout << "True" << endl; } else { cout << "False" << endl; } return 0; }

16th Mar 2020, 5:16 PM
Forsythe
3 Antworten
+ 2
Seems c++ casts f to double by adding 4 bytes to the end of f. This bytes can contain any numbers and it entails inequality
16th Mar 2020, 5:35 PM
Дмитрий Березин
+ 2
hmm try putting an f next to the second 0.1 maybe (0.1f). I'm afraid that because 0.1 is a double while f is a float might have as result a weird implicit conversion
16th Mar 2020, 5:21 PM
xaralampis_
xaralampis_ - avatar
+ 1
thanks!
16th Mar 2020, 5:36 PM
Forsythe