2017-01-14 82 views
1

我試圖確定e的連續數字中的前10位數的素數,因此想要將e計算爲任意數量的小數點。這是我的代碼,但是當我輸入1000例如,它不能階乘轉換成浮動,因爲它太大了:如何解決int太長,以轉換爲浮動錯誤?

import math 

def calculate_e(n): 
    e_real=0.0 
    e_test=0.0 
    accurate=False 
    term=1 
    while accurate==False: 
     e_real=sum(1/float(math.factorial(i)) for i in range(term)) 
     if (e_real-e_test)<n: 
      accurate=True 
     else: 
      accurate=False 
     term+=1 
     e_test=e_real 
    return e_real 

a=input("Enter accuracy: ")+1 
b=10**(-a) 
e=round(calculate_e(b),a) 
print e 

此外,另外我遇到的麻煩事正在打印這個數字沒有舍入的數字。假設我想查看e的前3位數字,我希望它輸出271,而不是272.我能夠提出的最好的方法是讓它精確到1位,並將它舍入到小數點後一位。

+1

你可以看看[decimal](https://docs.python.org/2/library/decimal.html)模塊,雖然你會獲得性能上的提升(如果這對你很重要)。 – SethMMorton

+0

可能的重複:http://stackoverflow.com/q/10056797 –

+3

@LaurentLAPORTE雖然確實OP可能要使用該問題的答案中建議的函數,但我不確定它是否是重複的,因爲這問題是關於繞過浮動的限制,而不是計算浮動的階乘。 – SethMMorton

回答

2

因子變得太大無所謂。您之前有更重要的問題方法:

>>> import math 
>>> for term in range(30): 
     e_real = sum(1/float(math.factorial(i)) for i in range(term)) 
     print '%2d %.60f' % (term, e_real) 

0 0.000000000000000000000000000000000000000000000000000000000000 
1 1.000000000000000000000000000000000000000000000000000000000000 
2 2.000000000000000000000000000000000000000000000000000000000000 
3 2.500000000000000000000000000000000000000000000000000000000000 
4 2.666666666666666518636930049979127943515777587890625000000000 
5 2.708333333333333037273860099958255887031555175781250000000000 
6 2.716666666666666341001246109954081475734710693359375000000000 
7 2.718055555555555447000415369984693825244903564453125000000000 
8 2.718253968253968366752815200015902519226074218750000000000000 
9 2.718278769841270037233016410027630627155303955078125000000000 
10 2.718281525573192247691167722223326563835144042968750000000000 
11 2.718281801146384513145903838449157774448394775390625000000000 
12 2.718281826198492900914516212651506066322326660156250000000000 
13 2.718281828286168710917536373017355799674987792968750000000000 
14 2.718281828446759362805096316151320934295654296875000000000000 
15 2.718281828458230187095523433526977896690368652343750000000000 
16 2.718281828458994908714885241352021694183349609375000000000000 
17 2.718281828459042870349549048114567995071411132812500000000000 
18 2.718281828459045534884808148490265011787414550781250000000000 
19 2.718281828459045534884808148490265011787414550781250000000000 
20 2.718281828459045534884808148490265011787414550781250000000000 
21 2.718281828459045534884808148490265011787414550781250000000000 
22 2.718281828459045534884808148490265011787414550781250000000000 
23 2.718281828459045534884808148490265011787414550781250000000000 
24 2.718281828459045534884808148490265011787414550781250000000000 
25 2.718281828459045534884808148490265011787414550781250000000000 
26 2.718281828459045534884808148490265011787414550781250000000000 
27 2.718281828459045534884808148490265011787414550781250000000000 
28 2.718281828459045534884808148490265011787414550781250000000000 
29 2.718281828459045534884808148490265011787414550781250000000000 

這些是在e_real中計算的確切值。你可以看到,當term變成18或更大時,你的e_real不再改變。花車根本沒有能力存儲接近1000個有效數字的地方。

正如SethMMorton所說,請查看decimal模塊。這可以解決你的兩個問題。

+0

乾杯。是的,我也沒有考慮過。我還是很新的,所以我想我並沒有完全理解浮動是什麼以及它和小數之間的區別。我也問過SethMMorton,但是小數如何能夠處理任意大的整數,但是浮點數是不是? – user7389469

+0

@ user7389469因爲浮點數(通常)是標準的64位浮點數,而小數點被實現爲「數組數組」(類似的數組),您可以在其中指定大小。 –

+0

這是有道理的,非常感謝:) – user7389469

相關問題