2010-08-24 361 views

回答

20

你可以设置你想要的精确decimal内置模块

from decimal import * 
getcontext().prec = 40 
Decimal(1).exp() 

这将返回:

Decimal('2.718281828459045235360287471352662497757') 
+1

+1;对于任何怀疑者,如果这可以达到任意的精确度,请检查我的答案。 – 2010-08-27 13:38:46

8

这也可以使用numerical evaluationsympy来完成:

import sympy 

print sympy.N(sympy.E, 100) 
+4

或使用sympy在原文中使用的原始http://code.google.com/p/mpmath/。 非常好的Python函数库,用于具有大量函数的任意精度浮点运算,Frederic的博客很有趣,可以阅读 – user333700 2010-08-25 02:01:52

3

我想你可以在信息从这些网页合并:

http://en.wikipedia.org/wiki/Taylor_series

这给了你熟悉的幂级数。由于您使用的是大因子数,因此您应该使用实现多重积分算术的gmpy。圆周率的评价的示例如下

http://wj32.wordpress.com/2007/12/04/ways-to-calculate-pi-in-python/

+0

_Power series_(通常包括Taylor Maclaurin系列),但是。请参阅http://en.wikipedia.org/wiki/E_%28mathematical_constant%29#Representations以获取有关'e'表示的详细信息。 – mctylr 2010-08-24 18:53:19

7

给出使用一系列的总和,你可以计算出它:

getcontext().prec = 2000 
e = Decimal(0) 
i = 0 
while True: 
    fact = math.factorial(i) 
    e += Decimal(1)/fact 
    i += 1 
    if fact > 10**2000: break 

但是,这不是真的有必要,因为什么Mermoz酒店没有同意它只是罚款:

>>> e 
Decimal('2.7182818284590452353602874713526624977572470936999595749669676 
277240766303535475945713821785251664274274663919320030599218174135966290 
435729003342952605956307381323286279434907632338298807531952510190115738 
341879307021540891499348841675092447614606680822648001684774118537423454 
424371075390777449920695517027618386062613313845830007520449338265602976 
067371132007093287091274437470472306969772093101416928368190255151086574 
637721112523897844250569536967707854499699679468644549059879316368892300 
987931277361782154249992295763514822082698951936680331825288693984964651 
058209392398294887933203625094431173
823764648042953118023287825098194558153017567173613320698112509961818815 
930416903515988885193458072738667385894228792284998920868058257492796104 
841984443634632449684875602336248270419786232090021609902353043699418491 
463140934317381436405462531520961836908887070167683964243781405927145635 
490613031072085103837505101157477041718986106873969655212671546889570350 
681701210056278802351930332247450158539047304199 
577770935036604169973297250886876966403555707162268447162560798826517871 
341951246652010305921236677194325278675398558944896970964097545918569563 
802363701621120477427228364896134225164450781824423529486363721417402388 
934412479635743702637552944483379980161254922785092577825620926226483262 
779333865664816277251640191059004916449982893150566047258027786318641551 
956532442586982946959308019152987211725563475463964479101459040905862984 
967912874068705048958586717479854667757573205681288459205413340539220001 
137863009455606881667400169842055804033637953764520304024322566135278369 
511778838638744396625322498506549958862342818997077332761717839280349465 
014345588970719425863987727547109629537415211151368350627526023264847287 
039207643100595841166120545297030236472549296669381151373227536450988890 
313602057248176585118063036442812314965507047510254465011727211555194866 
850800368532281831521960037356252794495158284188294787610852639810') 
>>> Decimal(1).exp() 
Decimal('2.7182818284590452353602874713526624977572470936999595749669676 
277240766303535475945713821785251664274274663919320030599218174135966290 
435729003342952605956307381323286279434907632338298807531952510190115738 
341879307021540891499348841675092447614606680822648001684774118537423454 
424371075390777449920695517027618386062613313845830007520449338265602976 
067371132007093287091274437470472306969772093101416928368190255151086574 
637721112523897844250569536967707854499699679468644549059879316368892300 
987931277361782154249992295763514822082698951936680331825288693984964651 
058209392398294887933203625094431173
823764648042953118023287825098194558153017567173613320698112509961818815 
930416903515988885193458072738667385894228792284998920868058257492796104 
841984443634632449684875602336248270419786232090021609902353043699418491 
463140934317381436405462531520961836908887070167683964243781405927145635 
490613031072085103837505101157477041718986106873969655212671546889570350 
681701210056278802351930332247450158539047304199 
577770935036604169973297250886876966403555707162268447162560798826517871 
341951246652010305921236677194325278675398558944896970964097545918569563 
802363701621120477427228364896134225164450781824423529486363721417402388 
934412479635743702637552944483379980161254922785092577825620926226483262 
779333865664816277251640191059004916449982893150566047258027786318641551 
956532442586982946959308019152987211725563475463964479101459040905862984 
967912874068705048958586717479854667757573205681288459205413340539220001 
137863009455606881667400169842055804033637953764520304024322566135278369 
511778838638744396625322498506549958862342818997077332761717839280349465 
014345588970719425863987727547109629537415211151368350627526023264847287 
039207643100595841166120545297030236472549296669381151373227536450988890 
313602057248176585118063036442812314965507047510254465011727211555194866 
850800368532281831521960037356252794495158284188294787610852639814') 
+0

你意识到系列总和的最后一位是错的? (0,应该是4)。 – kennytm 2010-08-24 18:44:16

+0

是的。 – 2010-08-24 18:59:36

1

使用贤者:

N(e, digits=2000) 
5

优秀的纯python库Mpmath肯定会诀窍。

该库的唯一重点是多精度浮点运算

例如,的mpath可以评估ë任意精度:

In [2]: from mpmath import * 
# set the desired precision on the fly 
In [3]: mp.dps=20; mp.pretty=True 
In [4]: +e 
Out[4]: 2.7182818284590452354 

# re-set the precision (50 digits) 
In [5]: mp.dps=50; mp.pretty=True 
In [6]: +e 
Out[6]: 2.7182818284590452353602874713526624977572470937 

顺便说一句,Mpmath也紧密Matplotlib集成。