0

I want to calculate pi with the chudnowsky algorithm and I want it to be precise so
I tried using Decimal module but I couldnt understand how I can use it. The code is

import decimal as dc
import math
dc.getcontext().prec = 54
sum1 = 0
for k in range(0, 100):
    a = (-1)**k*(math.factorial(6*k))*(13591409+545140134*k)
    b = (math.factorial(3*k))*(math.factorial(k))**(3)*(640320**(3*k))
    sum1 += a/b

sum1 = sum1/(426880*dc.Decimal("10005")**(1/2))
sum1= sum1**(-1)
print(sum1)

but it gives

Exception "unhandled TypeError"
unsupported operand type(s) for ** or pow(): 'decimal.Decimal' and 'float'

I look the internet but couldnt find a good source that covers the things and gives examples. How can I square root decimals and if I want a Decimal type result and the operations to be mostly lossless how can I implement it to the code above? Its python 3.6 I am new here so I thank you for being tolerant to me.

Patrick Artner
  • 50,409
  • 9
  • 43
  • 69
  • Possible duplicate of [Python pi calculation?](https://stackoverflow.com/questions/28284996/python-pi-calculation) which also uses chudnowsky – Patrick Artner Feb 20 '19 at 15:43

2 Answers2

2

I don't have too much experience with decimal, but the error you get is because you can't use the ** operator with a decimal and a float. The solution is to make the exponent a Decimal too so instead of dc.Decimal("10005")**(1/2) you can do dc.Decimal(10005)**dc.Decimal(1/2).

Basically, you can't combine Decimals and floats but integers are not a problem. You only need to be careful when you divide integers because that will result in floats, so you also need to change sum1 += a/b to sum1 += dc.Decimal(a)/b. When one operand is a Decimal the result will also be a Decimal. All the other numbers in your program are integers anyway so now it should work fine.

1

The module decimal does not make sense to be used here. You are using math.xxxx functions, I cite the module documentation:

The following functions are provided by this module. Except when explicitly noted otherwise, all return values are floats.

Source: https://docs.python.org/3/library/math.html

math.factorial - returns float

Beside that: you recalculate constants:

dc.Decimal("10005")**(1/2) == 5002.5

No need to recalculate it - simply use dc.Decimal('5002.5') instead

Patrick Artner
  • 50,409
  • 9
  • 43
  • 69