I wrote a program which takes an integer k and calculates geometric sum till 2^k.
using namespace std;
long double recursive( long double n){
if(n==1){
return 1;
}
long double ans = recursive(2*n);
return n+ans;
}
int main() {
int k;
cin>>k;
int n = 1<<k;
long double d = 1/long double(n);
cout<<recursive(d)<<endl;
return 0;
}
Here while converting n(integer) to long double
long double d = 1.0/long double(n);
I'm getting the following error
error: expected primary-expression before ‘long’
long double d = 1.0/long double(n);
^~~~
Whereas when I declare
double d = 1.0/double(n);
The program runs without any error. Why do I get error when I declare as long double?
I know I can make n as long double while declaring itself, but I wanted to know the reason for this error and how to solve this .