-3

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 .

dL1ght
  • 3
  • 2
  • 1
    I would do `long double d = 1.0L / n;`. – 273K Jan 06 '22 at 05:51
  • 1
    One more reason to not use function-like casts. If you really need to cast between types use e.g. `static_cast`. – Some programmer dude Jan 06 '22 at 05:51
  • How about `long double d = 1.0 / n`? –  Jan 06 '22 at 05:52
  • Or, considering that you're using a literal value just make it a floating point value like `1.0 / n`. And you almost never need to use `long double` (on some system with some compilers, there's actually no difference between `double` and `long double`). – Some programmer dude Jan 06 '22 at 05:53
  • @Someprogrammerdude Ig it'll work using static cast. I just wanted to know why we can type cast n into double but not long double :( – dL1ght Jan 06 '22 at 06:07
  • It's not possible to use function-like casts using types that have multiple keywords, like `long double` or `unsigned int`. – Some programmer dude Jan 06 '22 at 06:21

1 Answers1

0

This is something inherited from C. While doing the code analysis, the compiler sees "long" and "double" as two separate words. The same would occur for something like "unsigned int".

There are multiple ways to achieve what you want. In C you would cast it

long double my_var = 1/(long double)4;

C++ supports this because of legacy compatibility, but it's not compile time checked.

C also provides floating point literals, that are also in C++

long double my_var = 1/4.0L;

In C++ you could make your own alias to the double word type with a using statement

using LongDouble = long double;

auto my_var{1/LongDouble{4}};

But the preferred C++ way is to use static_cast

auto my_var{1/static_cast<long double>(4)};
JHBonarius
  • 10,824
  • 3
  • 22
  • 41
  • Now I understood , thanks. Will Use static cast from next time – dL1ght Jan 07 '22 at 11:13
  • @dL1ght you're welcome. In C++ we try to be type strict and use explicit conversions. There have been real-world serious issues caused by types not being what you expect. E.g. overflow errors. – JHBonarius Jan 07 '22 at 11:38
  • Hmm, I'll keep that in mind, Thanks @JHBonarius – dL1ght Jan 10 '22 at 12:48