1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
| #!/usr/bin/python
# -*- coding: utf-8 -*-
from __future__ import division
from decimal import *
##############################################################################
def pidec():
"""Calcul de pi avec la précision demandé"""
getcontext().prec += 2
a1 = Decimal("1.0")
b1 = Decimal("1.0")/Decimal("2.0").sqrt()
p1 = Decimal("1.0")
t1 = Decimal("0.25")
eps = Decimal("1.0e-" + str(getcontext().prec-1)) # condition d'arrêt
while True:
a2 = (a1 + b1)/2
b2 = (a1*b1).sqrt()
p2 = 2*p1
a12 = a1-a2
t2 = t1-p1*a12*a12
if abs(a2-b2)<eps:
break
else:
a1, b1, p1, t1 = a2, b2, p2, t2
ab = a2+b2
res = (ab*ab)/(4*t2)
getcontext().prec -= 2
return res*1 |
Partager