Question

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:)

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top