Plz look at the following code and answer why its happening?? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Plz look at the following code and answer why its happening??

Is same value of float is less than double? Run code for different values (specially 0.5 , 0.7, 0.9, 0.4) https://code.sololearn.com/c6Ru8kPquUOk/?ref=app https://code.sololearn.com/c6Ru8kPquUOk/?ref=app

26th Nov 2019, 8:50 AM
Dnyaneshwar
Dnyaneshwar - avatar
8 Answers
+ 1
Yes. Default type of precision numbers are double not float. You comparing float with double which is higher type So not same. Float require 4bytes while double occupies 8bytes Dnyaneshwar got it?
26th Nov 2019, 9:43 AM
Jayakrishna 🇮🇳
+ 1
Yes absolutely correct but when I run code for 0.5 and 0.6, 0.7 it's showing contradictory answer with theory. Plz explain??
26th Nov 2019, 1:54 PM
Dnyaneshwar
Dnyaneshwar - avatar
+ 1
https://code.sololearn.com/c1dvpsoA6aqz/?ref=app look at this code for double comparition
26th Nov 2019, 1:56 PM
Jayakrishna 🇮🇳
+ 1
https://code.sololearn.com/c6Ru8kPquUOk/?ref=app As of your answer my code should print hello. But it's printing Good.
26th Nov 2019, 2:01 PM
Dnyaneshwar
Dnyaneshwar - avatar
+ 1
Dnyaneshwar my point to is when you compre with float value with direct decimal number, they not same like 0.7 is default double type. float is less compare to double.. a=0.7f a<0.7 is true and for 0.8 it is happening because of compiler specific.. and am confused wait please....
26th Nov 2019, 2:21 PM
Jayakrishna 🇮🇳
+ 1
Dnyaneshwar Ok. I referred others ,to give you a better answer. That's why some delay... With refference from Gawen Steasy and ~swim~ : Answer is floating point comparisons are unpredictable , different compiler produce different results. because it is compiler implementation specific... 0.6, 0.7 all are by default double values... here in your case, Odd. 0.9 It is checking, if a<0.9 i.e 0.899999762<0.9 Even.... 0.8 It is checking if a<0.8 i.e 0.80000000<0.800000119 https://code.sololearn.com/cKii0a2b11M9/?ref=app if you still need some other details mention here...
26th Nov 2019, 5:26 PM
Jayakrishna 🇮🇳
0
Thank you Jaya krishna . Fully satisfied with your answer. Thanks for helping 😊
3rd Dec 2019, 6:30 AM
Dnyaneshwar
Dnyaneshwar - avatar
0
Dnyaneshwar Wel come..
3rd Dec 2019, 8:58 AM
Jayakrishna 🇮🇳