0

Can you explain to me?

#include <iostream> using namespace std; int main(){ double a=0.1; double b=0.2 double c= 0.3; What is the result of these statements: 1. if (a+b==c) cout<<1; else cout<<0; 2. If ((a+b)==c) cout<<1; else cout<<0;

6th Jan 2022, 3:47 AM
Oliver Pasaribu
Oliver Pasaribu - avatar
2 ответов
+ 3
0,0 Since, a+b == c returns False else part is executed. https://www.sololearn.com/discuss/2011017/?ref=app
6th Jan 2022, 4:53 AM
Simba
Simba - avatar
0
Ok. We all know that double is more precision than float. But in this case, we define double a=0.1, double b= 0.2, double c= 0.3. Let us evaluate: a+b = 0.1 + 0.2 = 0.3 exact result. Because you can say that 0.3 = 0.3000000 = 0.3000001 or 0.3000005 for example. If we take a limit, 0.3000000, 0.3000000001, 0.300000000005 , the limit must be 0.3. The more decimal or digit number after the point ., Then we knew that the number Will indicate the precision result. But once more, we defined double a= 0.1, can be 8 digit 0.10000000, or event 16 digit 0.1000000000000000, but all of above which has 8 or 16 significant number still 0.1. So it should execute the true part of if clause.what do you think? If you don't believe me, now let define double d= 0.0 as addition to a,b,c. Let d=a+b = 0.1+0.2=0.3. and c=0.3. Try this: cout<<d==c or cout<<c==d. Evaluate the result.
6th Jan 2022, 9:09 AM
Oliver Pasaribu
Oliver Pasaribu - avatar