Why these C++ code output false? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Why these C++ code output false?

float f = 0.1; if(f == 0.1) cout<<“True”; else cout<<“False”;

27th Aug 2022, 2:51 PM
范祥彥
范祥彥 - avatar
2 Answers
+ 1
0.1 is a double type value. When you store it in float type which is 4bytes of storage, it is converted and conversion value will have some difference in precision value. So difference in value exists because of floating point conversions.. edit: my sample test codes, which have more information, in comments. may that helps you understand it clearly.. read once.. hope it helps.. https://code.sololearn.com/c1dvpsoA6aqz/?ref=app https://code.sololearn.com/cKii0a2b11M9/?ref=app
29th Aug 2022, 8:15 AM
Jayakrishna 🇮🇳
+ 1
Thank you for so much information! It is good to know the difference of data structure between float and double type by your programs, mentors are always helpful and powerful in Sololearn community! Really appreciate and sorry for reply late.
30th Aug 2022, 1:24 PM
范祥彥
范祥彥 - avatar