pi计算中的分段错误(python)(segmentation fault in pi calculation (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退出。 我怎样才能避免这种情况? 它为什么会崩溃? 不能只是请使用更多的内存或什么?
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*2Anywhere 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中发现了一个无法处理疯狂长表达式的错误:
>>> eval("1.0*"*10000+"1.0") 1.0 >>> eval("1.0*"*100000+"1.0") # segfault here不过,我尽量使用“疯狂地长”这个词。 不要这样做,随着时间的推移计算碎片。 在这种情况下没有理由使用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 hereI 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.
更多推荐
发布评论