我试图只打印选定数量的Pi,它返回错误“Decimal has no attribute:\uugetitem”__

2024-09-30 14:31:00 发布

您现在位置:Python中文网/ 问答频道 /正文

def pi():
    prompt=">>> "
    print "\nWARNING: Pi may take some time to be calculated and may not always be correct beyond 100 digits."
    print "\nShow Pi to what digit?"
    n=raw_input(prompt)
    from decimal import Decimal, localcontext
    with localcontext() as ctx:
        ctx.prec = 10000
        pi = Decimal(0) 
        for k in range(350): 
            pi += (Decimal(4)/(Decimal(8)*k+1) - Decimal(2)/(Decimal(8)*k+4) - Decimal(1)/(Decimal(8)*k+5) - Decimal(1)/(Decimal(8)*k+6)) / Decimal(16)**k
    print pi[:int(n)]
pi()




Traceback (most recent call last):
  File "/Users/patrickcook/Documents/Pi", line 13, in <module>
    pi()
  File "/Users/patrickcook/Documents/Pi", line 12, in pi
    print pi[:int(n)]
TypeError: 'Decimal' object has no attribute '__getitem__'

Tags: toinpibeuserspromptmayfile
3条回答

如果你想要更快的pi算法,试试这个。我以前从未使用过Decimal模块;我通常使用mpmath来进行任意精度的计算,它附带了很多函数,并为pi和e内置了“常量”。但我想Decimal是一个方便的工具,因为它是一个标准模块。在

''' The Salamin / Brent / Gauss Arithmetic-Geometric Mean pi formula.

    Let A[0] = 1, B[0] = 1/Sqrt(2)

    Then iterate from 1 to 'n'.
    A[n] = (A[n-1] + B[n-1])/2
    B[n] = Sqrt(A[n-1]*B[n-1])
    C[n] = (A[n-1]-B[n-1])/2

    PI[n] = 4A[n+1]^2 / (1-(Sum (for j=1 to n; 2^(j+1))*C[j]^2))

    See http://stackoverflow.com/q/26477866/4014959

    Written by PM 2Ring 2008.10.19
    Converted to use Decimal 2014.10.21
    Converted to Python 3 2018.07.17
'''

import sys
from decimal import Decimal as D, getcontext, ROUND_DOWN

def AGM_pi(m):
    a, b = D(1), D(2).sqrt() / 2
    s, k = D(0), D(4)

    for i in range(m):
        c = (a - b) / 2
        a, b = (a + b) / 2, (a * b).sqrt()

        s += k * c * c

        #In case we want to see intermediate results
        #if False:
            #pi = 4 * a * a / (1 - s)
            #print("%2d:\n%s\n" % (i, pi))
        k *= 2
    return 4 * a * a / (1 - s)

def main():
    prec = int(sys.argv[1]) if len(sys.argv) > 1 else 50

    #Add 1 for the digit before the decimal point,
    #plus a few more to compensate for rounding errors.
    #delta == 7 handles the Feynman point, which has six 9s followed by an 8
    delta = 3
    prec += 1 + delta

    ctx = getcontext()
    ctx.prec = prec

    #The precision of the AGM value doubles on every loop
    pi = AGM_pi(prec.bit_length())

    #Round down so all printed digits are (usually) correct
    ctx.rounding = ROUND_DOWN
    ctx.prec -= delta
    print("pi ~=\n%s" % +pi)

if __name__ == '__main__':
    main()

我对这个过程花了多长时间感到厌烦(350次迭代循环是个杀手),但答案似乎很简单。一个Decimal对象不能按您的方式订阅。在

您可能需要先将其转换为字符串,然后再进行处理以获得数字:

print str(pi)[:int(n)+1]   # ignore decimal point in digit count.

您还应该记住,这个会截断值,而不是四舍五入。例如,当PI开始为:

^{pr2}$

(大概是我脑子里能记住的),将字符串截断为5个有效数字将得到3.1415,而不是更正确的3.1416。在

您试图将pi视为一个数组,而它是Decimal。我想你在找quantizehttps://docs.python.org/2/library/decimal.html

相关问题 更多 >