Unexpected!

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?

3 Likes

variable ans is either uninitialized or not set to 0 ?

1 Like

its initialized and second one is giving wrong answer, not the first one.

1 Like

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.

6 Likes

I think that is the correct reason it is giving a wrong answer.

2 Likes

:face_with_hand_over_mouth:

You can confirm by type casting the whole poduct into long long.