+ 3

Can anyone solve this misunderstanding???

Question : double a= 0.1; double b=0.2; double c=0.3; If (a+b==c) Cout<<"yes"; Else Cout<<"no"; //Why no it's outputed?? //Is it because it's value is same but reference is different like variable a,b and c are different references??

25th Apr 2020, 7:04 AM
The CodeGeek
The CodeGeek - avatar
7 odpowiedzi
+ 6
Yes
25th Apr 2020, 8:46 AM
The CodeGeek
The CodeGeek - avatar
+ 6
Yes .. Actually I forgot to do that
25th Apr 2020, 11:40 AM
The CodeGeek
The CodeGeek - avatar
+ 4
Is it reference concept .. If when it is reference and when value??
25th Apr 2020, 7:05 AM
The CodeGeek
The CodeGeek - avatar
+ 3
https://code.sololearn.com/cpcn5vq99O29/?ref=app https://code.sololearn.com/clm1vHX8oEYf/?ref=app Just see this I have done these in C and CPP so that u can find the difference. Actually 0.1+0.2 is not equal to 0.3 since it's equal to 0.3000000000004 something.. so just check this one Prathik Shetty
27th Apr 2020, 7:48 AM
Nikhil Maroju
Nikhil Maroju - avatar
+ 2
It's only a matter of values, here. The thing is that c is not initialized, that means that it holds some garbage value (or 0, I don't know about the C++ behavior about uninitialized value). Whatever, a+b is 0.3 and c is unknown (and you will have only a few chances to have c = 0.3) so a+b != c : it outputs "no".
25th Apr 2020, 7:53 AM
Théophile
Théophile - avatar
+ 2
Théophile c is initialized with 0.3?
25th Apr 2020, 8:45 AM
Abhay
Abhay - avatar
+ 2
But wait... You changed your question... When I answered to it, c wasn't initialized to 0.3.
25th Apr 2020, 11:33 AM
Théophile
Théophile - avatar