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

Правка en1, от 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 - Johnny and Another Rating Drop Any help is appreciated. Thank You

Теги pow, c++

История

 
 
 
 
Правки
 
 
  Rev. Язык Кто Когда Δ Комментарий
en1 Английский SagarPrasad 2021-10-27 10:47:31 773 Initial revision (published)