segmentation fault in pi calculation (python)(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)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:pi 计算中的分段错误(python)


基础教程推荐
- 如何让 python 脚本监听来自另一个脚本的输入 2022-01-01
- 筛选NumPy数组 2022-01-01
- 在 Python 中,如果我在一个“with"中返回.块,文件还会关闭吗? 2022-01-01
- 何时使用 os.name、sys.platform 或 platform.system? 2022-01-01
- 用于分类数据的跳跃记号标签 2022-01-01
- 如何在海运重新绘制中自定义标题和y标签 2022-01-01
- 使用PyInstaller后在Windows中打开可执行文件时出错 2022-01-01
- 线程时出现 msgbox 错误,GUI 块 2022-01-01
- Dask.array.套用_沿_轴:由于额外的元素([1]),使用dask.array的每一行作为另一个函数的输入失败 2022-01-01
- Python kivy 入口点 inflateRest2 无法定位 libpng16-16.dll 2022-01-01