(0.7 >a) return true , can you explain why ? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

(0.7 >a) return true , can you explain why ?

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

14th Aug 2019, 3:44 AM
Spring
5 Answers
+ 1
You got the answer then plzz show🙂🙂🙂
14th Aug 2019, 5:01 AM
Sourajeet Dey
Sourajeet Dey - avatar
+ 6
0.7 is a double value. Putting it in a float losses precision so the float ends up smaller. Your best practice for dealing with real numbers is use double as your type. Also, it doesn't hurt to use a tiny range for comparisons. If (a > 0.69999 && a < 0.70001) works better than if (a == 0.7).
14th Aug 2019, 3:56 AM
John Wells
John Wells - avatar
+ 4
Due to something like 0.699999999999 > 0.69999. The number of decimal places I used isn't exact but just to give you a general idea.
14th Aug 2019, 7:39 AM
Sonic
Sonic - avatar
+ 1
Thank you sir .
14th Aug 2019, 4:09 AM
Spring
+ 1
Program needs to convert this: 0.7 into binary: 0.10111111111111111... You can not write up the decimal expansion of 0.7 in binary. It has similar problem than trying to divide 1 with 3. When program writes it from the binary back to 0.7, it has a small throw.
14th Aug 2019, 5:36 AM
Seb TheS
Seb TheS - avatar