[docs] The computation of pi in Decimal fixed point and floating point arithmetic
bernard greening
brgreening at gmail.com
Fri Jun 29 00:15:15 CEST 2012
This is not a bug, its a question.
on the web page
http://docs.python.org/library/decimal.html
9.4 Decimal fixed point and floating point arithmetic
in the
9.4.7. Recipes¶ <http://docs.python.org/library/decimal.html#recipes>
section is a function to compute the value of pi
def pi():
"""Compute Pi to the current precision.
>>> print pi()
3.141592653589793238462643383
"""
getcontext().prec += 2 # extra digits for intermediate steps
three = Decimal(3) # substitute "three=3.0" for regular floats
lasts, t, s, n, na, d, da = 0, three, 3, 1, 0, 0, 24
while s != lasts:
lasts = s
n, na = n+na, na+8
d, da = d+da, da+32
t = (t * n) / d
s += t
getcontext().prec -= 2
return +s # unary plus applies the new precision
I would like to ask the author what algorithm this is based upon and any references as to how it works.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/docs/attachments/20120628/45d50598/attachment-0001.html>
More information about the docs
mailing list