6

I'm trying to calculate pi with arbitrary precision on Python using one of Ramanujan's formulas: http://en.wikipedia.org/wiki/Approximations_of_%CF%80#20th_century. It basically requires lots of factorials and high-precision floating numbers division.

Here's my code so far: http://pastie.org/private/pa6ijmoowiwiw4xwiqmq

I'm getting error somewhere around the fifteenth digit of pi( 3.1415926535897930 and it should be 3.1415926535897932 ). Can you give some advice why is it happening? I' am using decimal type and the docs say that it allows arbitrary precision floating and integer numbers.

PS: It's a homework assignment so i can't use another formula. PSS: I'm using python 2.7

Thanks:)

1
  • 2
    Do you realize that Decimal(sqrt(2)) is just a complicated way of doing sqrt(2) without the Decimal? Try with Decimal(2).sqrt() Commented Jun 3, 2013 at 8:09

1 Answer 1

3

Use Decimal(2).sqrt() instead of Decimal(sqrt(2)).

I've checked the first 1000 digits and it seems to work fine. By the way, for some reason your code outputs 1007 decimal places instead of 1000.

Sign up to request clarification or add additional context in comments.

1 Comment

Yup i add precision just to be sure. Thanks a lot:)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.