-1

I was trying to make a C++ program to calculate value of Pi up to 'n' decimal places using the Leibniz formula.

The number 'n' would be entered by the user. I was successful in creating the Leibniz formula but I am having trouble with the latter part i.e value precise up to 'n' places.

The trouble :- As more and more terms will continue to add to it, it's digits will keep on changing so how to tell if a particular digit has stopped changing despite the addition of more terms.

The code written so far :-

#include<iostream>
using namespace std;
int main()
{
    float s=0;
    int w=-1;
    for(float i=1;;i=i+2)
    {
        w=w*(-1);
        s=s+w*(1/i);
        cout<<s<<endl;
    }
    return 0;
}

It would be great if things would be kept simple since I am just a beginner at C++.

Thank you very much :)

Dmitry Bychenko
  • 180,369
  • 20
  • 160
  • 215
  • 1
    https://stackoverflow.com/questions/5694877/c-library-for-big-float-numbers – Dmitry Bychenko Feb 05 '18 at 10:54
  • 4
    _"I am having trouble"_ is not a problem description. This question is highly vague and incomplete. Please read the Help Centre to see what you're missing - chiefly, the code you've written so far and a specific problem. – underscore_d Feb 05 '18 at 10:54
  • either I am missing something or the digits changing has nothing to do with pi being transcendental. You could write a (poor) iterative estimation for 1.0 and still have digits changing forever – 463035818_is_not_an_ai Feb 05 '18 at 10:54
  • 1
    Please, expose sample code. Are you aware of numerical floating point issues e.g. [SO: Floating point inaccuracy examples](https://stackoverflow.com/q/2100490/7478597)? – Scheff's Cat Feb 05 '18 at 10:55
  • Surely Google is a better place for this? This problem has been studied since the 1950s. – Bathsheba Feb 05 '18 at 10:55
  • You really ought to avoid `using namespace std` - it is a bad habit to get into, and [can silently change the meaning of your program](/q/1452721) when you're not expecting it. Get used to using the namespace prefix (`std` is intentionally very short), or importing *just the names you need* into the *smallest reasonable scope*. Aside from that, look at `std::setprecision` (in the `` header). – Toby Speight Feb 05 '18 at 13:37

1 Answers1

4

Since you want to compute Pi up to arbitrary nth digit, you want a library for working with big float numbers; see

C++ library for big float numbers

the real trouble is that Leibniz formula is not useful in the context. The actual precision achieved can be estimated as the last term in the formula

  Pi / 4 = 1/1 - 1/3 + 1/5 - 1/7 + ... + (-1)**(n + 1) * 1 / (2 * n - 1) + ... 

If, for intance, you want Pi up to 100th digit it means that the residual (and the last term) should be less than 1e-100:

 1 / (2 * n - 1) < 1e-100
 2 * n  - 1 > 1e100
 n > 5e99

And as you can see 5e99 loops is by far too much for the modern (super-)computers

Dmitry Bychenko
  • 180,369
  • 20
  • 160
  • 215
  • Just one more thing sir, you mentioned that for accuracy upto 100 digits would mean that the last term must be less than 1e-100. But isn't there a possibility that many of those small terms would add up to increment that last decimal digit? – Mayank Mittal Feb 05 '18 at 11:24
  • @infinitely curious: No, it's known result (theorem) about *alternating series*; so called *alternating series estimation theorem*: the residual is less than the last term. – Dmitry Bychenko Feb 05 '18 at 11:27