pi计算中的分段错误(python)(segmentation fault in pi calculation (python))

编程入门 行业动态 更新时间:2024-10-10 19:19:58
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*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?

最满意答案

您似乎在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 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.

更多推荐

本文发布于:2023-07-31 08:48:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1342475.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:错误   python   pi   calculation   fault

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!