In given code float value(c) and if value (c) matches then why is the output false? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

In given code float value(c) and if value (c) matches then why is the output false?

https://code.sololearn.com/cM3yt1j32ffE/?ref=app

14th Oct 2021, 2:54 PM
King
King - avatar
4 Answers
+ 5
Krs 7 , put this line to your code just after variable c is declared and initialized. it will show more decimal places ... float c=10.1; printf("Double value: %.20f \n", c); ...
14th Oct 2021, 5:04 PM
Lothar
Lothar - avatar
+ 3
The problem is "floating point precision". As floats/ doubles/ decimals cannot be represent with infinite precision, comparing them for equality fails.
14th Oct 2021, 2:59 PM
Lisa
Lisa - avatar
+ 2
It may happen that they equal or it may happen that they don't – comparing floats isn't safe...
14th Oct 2021, 3:11 PM
Lisa
Lisa - avatar
0
Lisa but if I give 10.00 as float value it gives true as output.
14th Oct 2021, 3:02 PM
King
King - avatar