Why this is false? | SoloLearn: Learn to code for FREE!

+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; }

3/16/2020 5:16:34 PM

Forsythe

3 Answers

New Answer

+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

+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

+1

thanks!