# Approximation of pi

Given that $\frac{\pi^2}{6}=\sum_{n=1}^{\infty}\left(\frac{1}{n^2}\right)$, I have to write a program in C that finds an approximation of $\pi$ using the formula $S_n=\sum_{i=1}^{n}\left(\frac{1}{i^2}\right)$. Then the approximation is: $\sqrt{6\cdot S_n}$ Could you tell me the result for $n=100$ so I can check if my output is right?? Thanks in advance!

Could you tell me why calculating this backwards, it approximates better the sum?

-

The reason why calculating it backwards may be a good idea depends on the computer implementation of floating point numbers. If for instance you try to add a very small number (say $10^{-15}$) to a much larger number (say $1$), the result will still be $1$ due to the limited precision. Thus, it's always a good idea to start adding the smaller numbers first.

-
Now I noticed that calculating backwards it approximates better the sum ONLY for n>=10000..Why does this happen??? :/ – Mary Star Nov 1 '13 at 16:15
For smaller $n$, $\frac 1{n^2}$ is not so much smaller than $1$, so you don't lose as much precision. It is still better to add up backwards, but you may not notice. It is even better to pair the small terms and add the pairs, then pair the pairs and add them up, and so on, but it may not be worth the programming work. – Ross Millikan Nov 1 '13 at 16:41

Mathematica is a wonderful tool for these kinds of computations; it is worth learning how to use it, and its baby brother Wolfram Alpha.

Here is what I get: link

-
I have to print the results once declaring the variables as float and once as double. My results are for float: 3.1320766431412150 and for double: 3.1320765318091053. Could you tell me if these are right??? – Mary Star Oct 28 '13 at 18:53
No idea. Figuring out exactly what the errors will be for given IEEE representations is beyond the scope of this site, I'm afraid... there's no way for us to check without doing the entire assignment ourselves. – user7530 Oct 28 '13 at 18:58
If you post your source code at stackoverlow.com maybe the folks there can critique it. – user7530 Oct 28 '13 at 18:59