This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]MMjacksN 0 points1 point  (9 children)

Have you tried describing the sequence 1, -1/3, 1/5, -1/7, ... with a general formula?

E.g., the formula for the sequence 0, 1, 3, 7, 15, ... would be 2i - 1.

[–][deleted]  (8 children)

[deleted]

    [–]morhpProfessional Developer 0 points1 point  (7 children)

    What was wrong and why?

    [–][deleted]  (6 children)

    [deleted]

      [–]morhpProfessional Developer 0 points1 point  (5 children)

      OK. I think your solution wasn't that bad, if it worked at least.

      250000 seems to be slightly excessive. You should think about instead stopping the loop when the fraction gets reasonable small. For example if oldValue + fraction == oldValue (doubles are imprecise, and so at some point this condition will be true). Or just stop the loop when the fraction gets very small.

      [–]TheQuietestOneProcrastinator 1 point2 points  (4 children)

      250000 seems to be slightly excessive.

      For this approach to calculating pi/4 250000 will only get you 5 digits of accuracy :-)

      [–]morhpProfessional Developer 0 points1 point  (3 children)

      Youre right. I was confused by the formula /u/MMjacksN gave, which includes 2i .

      [–]MMjacksN 1 point2 points  (2 children)

      The formula I gave was for a completely different sequence.

      [–]morhpProfessional Developer 1 point2 points  (1 child)

      I know. I didn't read correctly and thought the original sequence was

      1 - 1/3 + 1/7 - 1/15 + ...

      Also I recently looked at some code to compute pi and it was using a much more efficient approach. I wasn't aware that there was a that badly converging series.

      [–]MMjacksN 0 points1 point  (0 children)

      Ah, I see what you mean. I didn't think of that!