# Thread: Approximating PI using the Taylor Series - Help!

1. ## Approximating PI using the Taylor Series - Help!

Hey. I want to write a program that approximates the value of PI using the Taylor Series. This is the program I've written so far but I'm not sure how to complete the FOR loop. Someone please help.

Code:
```#include <cstdlib>
#include <iostream>

using namespace std;

int main(int argc, char *argv[])
{
cout<<"Piapproximation\n"
"Welcome to my version of Piapproximation\n";
//declaring variables
int terms;
double pi = 0.0;
//getting input from user
cout<<"\nEnter the number of terms: ";
cin>> terms;
//loop to calculate the value of pi
for (long int i = 1; i <= terms; i ++)
...
//output
cout<<"\nThe approximation of PI is: "<< pi <<
"\n\nThank you!\n";

system("PAUSE");
return EXIT_SUCCESS;
}```

2. Use a flowchart to complete your logic, then translate to code.

3. I remember a π approximation we had done.We had used a do while loop,because the concept was to stop while the term was too small for us to be taken into account.We calculated the π which comes from the greek(of course) word περιφέρεια (=regional) by this series
Code:
`π²/6 = 1/1² + 1/2² + 1/3² + 1/4² + 1/5² + 1/6² + 1/7² + 1/8² + ...`
The tolerance is achieved by this
Code:
` while (currentTerm > 1.0e-15);  /* Stop if current term is very small */`
You can change the tolerance for more or less precision

Try writing your program and post back if needed

4. Dude, do not hand out solutions!

5. Originally Posted by Elysia
Dude, do not hand out solutions!
Edited post.Sorry Elysia,this is the second time i make the same mistake! :/

6. We calculated the π which comes from the greek(of course) word περιφέρεια (=regional) by this series
Given the definition of pi I find that odd. It has nothing to do with circumference and wikipedia says you're wrong. Are you really right?

7. Originally Posted by whiteflags
Given the definition of pi I find that odd. It has nothing to do with circumference and wikipedia says you're wrong. Are you really right?
Well i wasn't there when great greek mathematicians lived,so i will tell you what i was told .It is said that because if you select a random cycle and you calculate the fraction of its regional (=περιφέρεια) to its diameter (=διάμετρος) then you always get the number π.As a result π is said to be derived by the word περιφέρεια. It is also said that Epimarchos (=Επίμαρχος) invented the letter π in greek alphabet,but this is mythology.
So my answer is i do not know 100%,but this is a very interesting question.I will search it and i will post it to the general discussions thread if i have something.For now let's stick to the NewP's code and do not get much off topic

EDIt- another idea is that π could be derived by the greek word περίμετρος (=perimeter)

8. Originally Posted by std10093
Edited post.Sorry Elysia,this is the second time i make the same mistake! :/
Well, if you did it by mistake, then we're fine
We all make mistakes

9. Please have a go at writing the loop body and then post your attempt.

10. the taylor series is a really lousy approximation for pi though.

given the formula π²/6 = 1/1² + 1/2² + 1/3² + 1/4² + 1/5² + 1/6² + 1/7² + 1/8² + ... + 1/n²

n must be a VERY large number to get useful results.

11. Originally Posted by Elkvis
the taylor series is a really lousy approximation for pi though.

given the formula π²/6 = 1/1² + 1/2² + 1/3² + 1/4² + 1/5² + 1/6² + 1/7² + 1/8² + ... + 1/n²

n must be a VERY large number to get useful results.
That's not wrong.With the precision i stated before
Code:
`while (currentTerm > 1.0e-15);`
i got this result.

output
Code:
`Summed 31622777 terms, pi is 3.14159`
However this a school project,rather than a scientific application so i thought it would be ok

12. I wrote a multithreaded version that does it 40 billion times, and got 3.141592645... - should be 3.141592653...

given a sufficiently fast computer, you could calculate it within the precision of a double in a reasonable amount of time.

13. I calculated the absolute error of that(where x' is the machine number)
x' = 3.141592645

x= 3.141592653 (actual value)
Code:
`|ε| = |x - x'| = 0.000000008 = 0.08 * 10^(-7) < 0.5*10^(-7)`
which tell us that the machine value is going to be accurate at 7 decimal digits at the most.Here it is exact 7 digits.

Then i calculated the absolute relative error
Code:
`|ρ| = |ε| / x = 0,025464790899483937645941521750815 * 10^(-7) = 0,025464790899483937645941521750815 * 10^(-9) < 5 * 10^(-9)`
which says that at the most nine significant digits are going to be accurate.Here 8 digits are accurate.

However i think that this accuracy is satisfactory for a school exercise,so the series i gave before i think is enough

14. either I'm missing something, or you're leaving out a lot of information about the formulas you used to calculate that absolute relative error.

15. It says right there,
Code:
`|ρ| = |ε| / x = |x - x'| / x`