我是Python的初學者。我寫了一個計算10^-15的數字的函數。數值結果超出Python的範圍
在生成代碼輸出時,出現錯誤說「數值結果超出範圍」。發生此錯誤的可能原因是什麼?
這是我的代碼。
import numpy as np
import matplotlib.pyplot as plt
T=4.32*10**19
i=input("Number of iterations ")
h=T/i
a=[0.01]*6000
t=[0.0]*6000
data = np.loadtxt('/home/user/t.txt')
t=data[:]
def f (a):
if a!=0:
c=((0.75/a + 0.044/(a**2) + 0.74*(a**2))**0.5)
return c
else :
return 0
for n in range(0,5998):
k1=f (a[n]);
k2=f (a[n]+(h/2)*k1);
k3=f (a[n]+(h/2)*k2);
k4=f (a[n]+h*k3);
a[n+1]=(a[n]+(h/6)*(k1 + 2*k2 + 2*k3 + k4))
fo=open("a_of_t.txt", "w")
for item in a:
fo.write("%e\n" % item)
fo.close()
plt.plot(t,a, 'k')
plt.show()
錯誤讀取
line 23, in <module>
k3=f (a[n]+(h/2)*k2);
line 15, in f
c=((0.75/a + 0.044/(a**2) + 0.74*(a**2))**0.5)
OverflowError: (34, 'Numerical result out of range')
請顯示您的代碼以及您所獲得的特定錯誤消息(堆棧跟蹤)。 – yole
在一定的數量級上,花車將停止工作,但我忘了在哪裏。也許https://docs.python.org/2/library/decimal.html'decimal'會令你感興趣? – NightShadeQueen
它與[link](http://stackoverflow.com/questions/29488213/handling-very-small-numbers-in-python) –