Basic data type | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 3

Basic data type

Why it is not updated to double during comparison and double can't be compared completely. so, question is why answer of below code is 1 and not 2. int x=10; double y=10.0; if(x==y){cout <<"1";}else{cout<<"2";}

16th Mar 2018, 1:46 AM
Ketan Lalcheta
Ketan Lalcheta - avatar
2 Answers
+ 6
Variable x is implicitly casted to double during comparison. The value of x and y is compared, and found to be equal. Program prints 1.
16th Mar 2018, 2:10 AM
Hatsy Rei
Hatsy Rei - avatar
+ 2
correct. Type casting is what I was expecting.... can't we say that double generally don't get proper comparison due to precision values unlike int.... I mean to say that double x and double y should not match with comparison sign... I used to do double comparison as below: if(abs(x-y)<=0.0001) // This should be case instead of x==y
16th Mar 2018, 3:03 AM
Ketan Lalcheta
Ketan Lalcheta - avatar