本文介绍了pi计算中的分段错误(python)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
($):$ + = 2
seq.append(((%f ** 2)/(%f *%f))*%(float(counter),float(counter-1) )))
seq.append(1.0)
seq =.join(seq)
seq = eval(seq)
return seq * 2
在过去的85000个术语中,我遇到了分段错误和python退出。我怎样才能避免这一点?为什么会崩溃?不能只是请使用更多的内存或东西?
解决方案
您似乎已经找到错误在 eval
它不能处理疯狂的长表达式:
>>> eval(1.0 ** 10000+1.0)
1.0
>>> eval(1.0 ** 100000+1.0)
#segfault here
I不过,尽量使用疯狂的长这个短语。不要这样做,随着时间的推移计算这些碎片。在这种情况下没有理由使用 eval
。
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
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?
解决方案
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
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)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!