本文介紹了pi 計算中的分段錯誤(python)的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!
問題描述
def pi(times):
seq = []
counter = 0
for x in range(times):
counter += 2
seq.append("((%f**2)/(%f*%f))*"%(float(counter), float(counter-1), float(counter+1)))
seq.append("1.0")
seq = "".join(seq)
seq = eval(seq)
return seq*2
在超過 85000 個術語的任何地方,我都會遇到分段錯誤并且 python 退出.我怎樣才能避免這種情況?為什么會崩潰?就不能請使用更多的內存或其他東西嗎?
Anywhere past 85000 terms I get a segmentation fault and python quits. How can I avoid this? Why is it crashing? Can't it just please use more memory or something?
推薦答案
您似乎在 eval
中發現了一個錯誤,它無法處理超長的表達式:
You appear to have found a bug in eval
where it can't handle insanely long expressions:
>>> eval("1.0*"*10000+"1.0")
1.0
>>> eval("1.0*"*100000+"1.0")
# segfault here
不過,我還是明智地使用了非常長"這個短語.不要那樣做,邊走邊計算.在這種情況下沒有理由使用 eval
.
I use the phrase "insanely long" advisedly though. Don't do it that way, calculate the pieces as you go. There is no reason to be using eval
in this situation.
這篇關于pi 計算中的分段錯誤(python)的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!
【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!