Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

Value of pi(Need help with my code)

+6 votes
asked Apr 7, 2023 by WX23 (230 points)
I tried calculating the value of pi using this formula:

pi = lim as n approaches  ∞ of n*(sin(180/n))

I was able to get the value of pi accurate to 15 decimal places using a value of n=1e9. Any value of n beyond this yielded the same result, despite the fact that both pi and n were long doubles. Is there any way I can get a more accurate value of pi? Has anyone done this? What is the highest you guys have got?

1 Answer

+1 vote
answered Apr 13, 2023 by Peter Minarik (86,240 points)

In what language? What is the exact code you used?

In C/C++, the sin() function uses radians, so your formula does not work. You'd need to put PI/n in the arguments, but by then you would have already defined PI. :)

commented Apr 13, 2023 by WX23 (230 points)
Yeah, that’s the silly part about my code. I have to use c++ ’s predefined value M_PI to get my own approximation of pi. I managed to find e and phi to a few decimal places though.
commented Apr 13, 2023 by WX23 (230 points)
#include <iostream>
#include <cmath>
#include <iomanip>
#include <math.h>

using namespace std;
int main()
{
    long double n{1e9};
    long double pi{};
    long double e{};
    long double phi{};
    cout<<setprecision(16);
    
    pi=n*sin(M_PI/n);
    cout<<"pi = "<<pi<<endl;
    cout<<setprecision(9);
    
    e=pow((1+(1/n)),n);
    cout<<"e = "<<e<<endl;
    
    long double reps{};
    vector<long double> fib_nums;
    long double latest_num{};
    fib_nums.push_back(0);
    fib_nums.push_back(1);
    cout<<"How many Fibonacci numbers do you want to generate?"<<endl;
    cin>>reps;
    cout<<"Generating fibonacci numbers..."<<endl;
    cout<<"**************************************************"<<endl;
    cout<<"1"<<endl;
    for(long double i; i<(reps-1); i++){
        latest_num=fib_nums[fib_nums.size()-2]+fib_nums[fib_nums.size()-1];
        cout<<setprecision(30)<<latest_num<<endl;
        fib_nums.push_back(latest_num);
        
    }
    cout<<"**************************************************"<<endl;
    
    
}
 

Here’s my code. There’s a bit of other stuff for finding the values of other irrational numbers too.
commented Apr 14, 2023 by Peter Minarik (86,240 points)
pi=n*sin(M_PI/n);

Yeah, exactly what I suspected.

I'm not an expert in maths, but my understanding is that the sin wave approaches 0 as the argument approaches 0. lim n->0 sin(n) = 0.

So what your formula says is that when the sin wave input approaches 0, the sin wave becomes linear, so for this, we could say that lim alpha->0 sin(alpha) = linear(alpha), where linear is some kind of linear function. But since it's a linear function, linear(PI/n) can be written as linear(PI) / n.

To put everything together, n * sin(PI/n) approximated for lim n->∞ (i.e. lim PI/n->0) the function could be approximated with n * linear(PI) / n, where multiplication and division by n cancel each other so we're left with a linear function of PI, where the linear function really is just returning the argument.

In other words, if you'd use any number instead of M_PI, that number would be returned.

Hence what your code does, it just returns the constant defined by M_PI and you won't achieve any better accuracy.

Go ahead, and replace your code with any number and see what I'm talking about.

E.g.: pi=n*sin(1.23/n);
commented Apr 17, 2023 by WX23 (230 points)
I did this, and I did get 1.23. It works until I get close to the value of M_PI, and then just stops working. I think the only way to avoid this is to avoid using trigonometric functions like sin. I’ll probably have to check out another method :)
commented Apr 18, 2023 by Peter Minarik (86,240 points)
edited Apr 18, 2023 by Peter Minarik
Please, read this on how to calculate PI, it has a few methods: https://www.mathscareers.org.uk/calculating-pi/

Also, remember that double is 8 bytes and has 15-digit precision (depending on implementation), so it won't be able to correctly store more than these digits.

I got a bit carried away and did a quick project to calculate PI on my own. I selected the Nilakantha Series algorithm because it was convenient and used 128-bit floats for a bit more accuracy.

The below code can get up to 22-digit accuracy.

https://onlinegdb.com/QsHhigI_g

With more research, better algorithm one could do much better. With dedication, you can get to calculate even 1 million digits: https://www.piday.org/million/
Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...