Why does pow function needs to be typecasted in to int/long long int even for numbers with no decimal representation.

Revision en1, by SagarPrasad, 2021-10-27 10:47:31
void solve(){
    long long int n;
    cin>>n;
    long long int sol=0;
    for(int i=0;i<63;i++){
        sol=sol+n/pow(2,i);
    }
    cout<<sol<<endl;
}

Here for the test case 1 765228007342234864

solution is not coming correct and I need to typecast my pow(2,i) to (long long int)pow(2,i). For i=0, n/pow(2,i) is not even equal to n, why is it so?

As far as i know pow function gives value in double but even so the value of double should be 1.0 and hence n/(1.0) should be n, instead i am getting 765228007342234880 for the above test case which is more then n. 1362C - Джонни и ещё одно падение рейтинга Any help is appreciated. Thank You

Tags pow, c++

History

 
 
 
 
Revisions
 
 
  Rev. Lang. By When Δ Comment
en1 English SagarPrasad 2021-10-27 10:47:31 773 Initial revision (published)