What the answer is not equal here? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

What the answer is not equal here?

float f1 = 0.1; if(f1 == 0.1) printf("Equal"); else printf("Not equal"); O/P :- Not equal

21st Jul 2019, 12:11 PM
Suraj Patra
Suraj Patra - avatar
5 Answers
+ 2
Because floating point numbers have certain accuracy. Double type is more precise than float type. Please look at the code. Hope it helps you 😉 https://code.sololearn.com/c3S3k9To6R0D/?ref=app
21st Jul 2019, 12:15 PM
TheWh¡teCat 🇧🇬
TheWh¡teCat 🇧🇬 - avatar
+ 1
Because in you need to write "if(f1 == 0.1f)". because the "f" tells the compiler to use the number as a float instead of a double.
21st Jul 2019, 12:18 PM
haydenki
haydenki - avatar
+ 1
Thanks to all of you guys One more question What will store if we store 0.1 in float ? Means will it store as 0.1 or as 0.100...?
21st Jul 2019, 12:24 PM
Suraj Patra
Suraj Patra - avatar
+ 1
Suraj Patra 0.100..., because of accuracy of the representation.
21st Jul 2019, 12:25 PM
TheWh¡teCat 🇧🇬
TheWh¡teCat 🇧🇬 - avatar
+ 1
Thanks
21st Jul 2019, 12:28 PM
Suraj Patra
Suraj Patra - avatar