Take the 2-minute tour ×
Mathematics Stack Exchange is a question and answer site for people studying math at any level and professionals in related fields. It's 100% free, no registration required.

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?

share|improve this question

2 Answers 2

up vote 2 down vote accepted

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.

share|improve this answer
    
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
1  
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

share|improve this answer
    
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

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.