0

I try to calculating one series, using formula (2^n + 1) / (3^n - 1);

In the code below you can see how I do that:

#include <math.h> 
#include <iomanip>

using namespace std;

// 3.0856067145849229494715404431559591102995909750461578369140625 - {61 digits after dot} it's my record

int main() {
    int n = 1;
    char newl = '\n';
    long double res = (pow(2.0, n) + 1) / (pow(3.0, n) - 1);
    cout << fixed << setprecision(500) << res << newl;
    while (n < 10000) {
        n++;
        res += (pow(2.0, n) + 1) / (pow(3.0, n) - 1);
        cout << res << newl;
    }
    return 0;
}

And I want to improve my result, calcuting at least 100 digits after dot. Is that possible? If answer can be founded, give a way to solving this task, please. Sorry in advance for my bad English if I mistaked somewhere, thanks for all answers which will help and good luck to everyone.

theneon
  • 13
  • 2
  • You can get an arbitrary precision math library like GNU's [GMP](https://gmplib.org/). – Galik Mar 08 '22 at 14:04
  • 3
    all types in C++ have fixed precision – phuclv Mar 08 '22 at 14:05
  • Even if you did get it to print more, the calculation precision for IEE754 `double` is about 17 digits, so you need a library for actual precise calculations. – Yksisarvinen Mar 08 '22 at 14:08
  • Use library like boost multiprecision or gmp – drescherjm Mar 08 '22 at 14:15
  • Assuming long double is wider than double on your system, you might want to use the literals `2.0L` and `3.0L` to get the calculations done in long double. Now `pow(2.0, n)` returns a double. – BoP Mar 08 '22 at 14:15

0 Answers0