Hello,
Let a=800 and I print the answer in two ways:
1- ans=a \times pow(2,50) and cout<<ans<<endl;
2- and cout<<(a \times pow(2,50))<<endl;
2nd one gives the wrong answer on submission (In C++).
Explain why?
Hello,
Let a=800 and I print the answer in two ways:
1- ans=a \times pow(2,50) and cout<<ans<<endl;
2- and cout<<(a \times pow(2,50))<<endl;
2nd one gives the wrong answer on submission (In C++).
Explain why?
variable ans
is either uninitialized or not set to 0 ?
its initialized and second one is giving wrong answer, not the first one.
Could be wrong but maybe ans is a float datatype.
a * pow(2,50) will be double datatype because pow returns and requires double so what’s printed is a double whereas ans = a * pow(2,50) will typecast it into a float and cout that.
I think that is the correct reason it is giving a wrong answer.
You can confirm by type casting the whole poduct into long long.