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 退出。我怎样才能避免这种情况?为什么会崩溃?难道不能请使用更多内存或其他东西吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您似乎在
eval
中发现了一个错误,它无法处理超长的表达式:不过,我特意使用了短语“insanely long”。不要这样做,边做边计算。在这种情况下没有理由使用
eval
。You appear to have found a bug in
eval
where it can't handle insanely long expressions: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.为什么要使用
eval()
?做完全相同的事情。
Why use
eval()
at all?Does the exact same thing.