C++ / Python: Why is 1==1.0 true? Why is 1==1.0000000000000001 true? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

C++ / Python: Why is 1==1.0 true? Why is 1==1.0000000000000001 true?

From my understanding 1 is an integer type, while 1.0 and 1.0000000000000001 are floats. C++: #include <iostream> using namespace std; int main() { cout<< bool(1==1.0); // outputs 1 (True) return 0; } Python: print(1==1.0000000000000001) # outputs True Why are both of these evaluated as true?

18th Sep 2020, 6:43 PM
Solus
Solus - avatar
1 Answer
+ 1
for c++ the int gets converted to float before comparaison. for print(1==1.0000000000000001) on 64bit cpu the largest number of decimal digits you can use in a double is 15 and that number has 16 so the last digit 1 is omitted, so you get 1.0, remove one zero and you get a different number.
18th Sep 2020, 6:58 PM
Bahhaⵣ
Bahhaⵣ - avatar