Why is 1.1 not getting printed here? Please tell me! | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

Why is 1.1 not getting printed here? Please tell me!

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

21st Feb 2018, 12:09 AM
Nikhar Sachdeva
Nikhar Sachdeva - avatar
6 Answers
+ 3
Most floating point numbers don't have exact binary representation, so they should not be compared directly. in line 6, you should write: while(x==(float)1.1) EDIT: Check the link: https://stackoverflow.com/questions/8266878/why-floating-point-value-conditions-failed-in-if-condition
21st Feb 2018, 1:16 AM
777
777 - avatar
+ 2
And what should I search in Search engines to get further details about conversions like (float)1.1. implicit conversion or what is it called?.. I've just begun,so don't know much of the terminology
21st Feb 2018, 2:55 AM
Nikhar Sachdeva
Nikhar Sachdeva - avatar
+ 1
Beacause of the memory representation of float variables. 1.1 is represented as 1.0999999 in memory as binary numbers. @blue has suggested the correct possible solution to the problem.
21st Feb 2018, 2:42 AM
Harsh Kumar
Harsh Kumar - avatar
+ 1
I just tried what Blue has suggested and got this error. Error: Expected expression before float.
21st Feb 2018, 2:52 AM
Nikhar Sachdeva
Nikhar Sachdeva - avatar
+ 1
It worked fine on my device. Don't forget the parentheses () on float
21st Feb 2018, 2:56 AM
Harsh Kumar
Harsh Kumar - avatar
+ 1
These conversions are called explicit conversion meaning forceful conversion of data types.
21st Feb 2018, 2:57 AM
Harsh Kumar
Harsh Kumar - avatar