Python: mpmath 在除大数时失去精度

Python: mpmath lose precision when divide large number

我遇到了一个叫做 Chudnovsky 算法 的 π 算法。 Python 实现显示在 Wikipedia which use decimal package comes with Python. But recently when I test Gauss Legendre algorithm I found mpmath package runs much more efficient than decimal when deals with high precision calculation, so I hope the algorithm could work with mpmath. Here is my code:

#!/usr/bin/env python

from mpmath import *
import pi_compare # A module aim to compare result with standard pi

mp.dps = 1000
def pi():
    K, M, L, X, S = 6, mpf('1'), 13591409, 1, mpf('13591409')
    for i in xrange(0,100):
        M = (K**3 - K*16) * M / K**3
        L += 545140134
        X *= -262537412640768000
        S += (M * L) / X
        K += 12
    return mpf('426880') * mpf('10005').sqrt() / S

P = pi()
print P
print pi_compare.compare(str(P))

我相信 Python 本身可以处理大整数,所以我不在 var K, L, X 上使用 mpmath 因为在迭代中不会出现小数部分。我认为事情发生在 S += (M * L) / X 上,因为 X 是一个相当大的数字。 处理这么大的数字让我很困惑,希望你的建议,谢谢。

除了 DSM 提到的拼写错误外,该代码还有另一个问题。 k**3 的除法需要是 floor 除法,而不是 float 除法。这是使用 decimalmpmath 模块的修复版本。此代码适用于 Python 2 & Python 3.

from decimal import Decimal as Dec, getcontext as gc
from mpmath import mp

def pi_dec(maxK=70, prec=1008):
    gc().prec = prec
    K, M, L, X, S = 6, 1, 13591409, 1, 13591409 
    for k in range(1, maxK+1):
        M = (K**3 - (K<<4)) * M // k**3 
        L += 545140134
        X *= -262537412640768000
        S += Dec(M * L) / X
        K += 12
    pi = 426880 * Dec(10005).sqrt() / S
    return pi

def pi_mp(maxK=70, prec=1008):
    mp.dps = prec
    K, M, L, X, S = 6, 1, 13591409, 1, 13591409 
    for k in range(1, maxK+1):
        M = (K**3 - (K<<4)) * M // k**3 
        L += 545140134
        X *= -262537412640768000
        S += mp.mpf(M * L) / X
        K += 12
    pi = 426880 * mp.sqrt(10005) / S
    return pi


Pi = pi_dec()
print(Pi)

Pi = pi_mp()
print(Pi)

输出

3.14159265358979323846264338327950288419716939937510582097494459230781640628620899862803482534211706798214808651328230664709384460955058223172535940812848111745028410270193852110555964462294895493038196442881097566593344612847564823378678316527120190914564856692346034861045432664821339360726024914127372458700660631558817488152092096282925409171536436789259036001133053054882046652138414695194151160943305727036575959195309218611738193261179310511854807446237996274956735188575272489122793818301194912983367336244065664308602139494639522473719070217986094370277053921717629317675238467481846766940513200056812714526356082778577134275778960917363717872146844090122495343014654958537105079227968925892354201995611212902196086403441815981362977477130996051870721134999999837297804995105973173281609631859502445945534690830264252230825334468503526193118817101000313783875288658753320838142061717766914730359825349042875546873115956286388235378759375195778185778053217122680661300192787661119590921642019893809533 3.14159265358979323846264338327950288419716939937510582097494459230781640628620899862803482534211706798214808651328230664709384460955058223172535940812848111745028410270193852110555964462294895493038196442881097566593344612847564823378678316527120190914564856692346034861045432664821339360726024914127372458700660631558817488152092096282925409171536436789259036001133053054882046652138414695194151160943305727036575959195309218611738193261179310511854807446237996274956735188575272489122793818301194912983367336244065664308602139494639522473719070217986094370277053921717629317675238467481846766940513200056812714526356082778577134275778960917363717872146844090122495343014654958537105079227968925892354201995611212902196086403441815981362977477130996051870721134999999837297804995105973173281609631859502445945534690830264252230825334468503526193118817101000313783875288658753320838142061717766914730359825349042875546873115956286388235378759375195778185778053217122680661300192787661119590921642019893809523

为了比较,这里是 mp.pi 的值:

mp.dps = 1008
print(mp.pi)

3.14159265358979323846264338327950288419716939937510582097494459230781640628620899862803482534211706798214808651328230664709384460955058223172535940812848111745028410270193852110555964462294895493038196442881097566593344612847564823378678316527120190914564856692346034861045432664821339360726024914127372458700660631558817488152092096282925409171536436789259036001133053054882046652138414695194151160943305727036575959195309218611738193261179310511854807446237996274956735188575272489122793818301194912983367336244065664308602139494639522473719070217986094370277053921717629317675238467481846766940513200056812714526356082778577134275778960917363717872146844090122495343014654958537105079227968925892354201995611212902196086403441815981362977477130996051870721134999999837297804995105973173281609631859502445945534690830264252230825334468503526193118817101000313783875288658753320838142061717766914730359825349042875546873115956286388235378759375195778185778053217122680661300192787661119590921642019893809525