我正在编写一个 Python3(64 位)程序,以使用 64 位 Windows 10 上的 gmpy2 模块将 pi 计算到至少一百万位。我正在使用 Chudnovsky 算法。级数算术是天才,易于理解且易于实现。我遇到的问题是用 gmpy2 表示非常大的精度实数。
我把这个问题归结为一个非常简单的测试。
- 检查 gmpy2 系统限制。他们看起来很棒。
- 根据需要为一百万位 pi 设置精度上下文。
- 用一百万位 pi 分配一个字符串变量。
- 使用 mpfr('million digit pi') 为 mpfr 变量分配字符串转换
- 打印字符串变量,它有一百万位数字。
- 打印 mpfr 它有超过 300,000 位数字。
当我将 mpfr 转换为字符串并打印时,我得到了类似的结果。
这里发生了什么,我如何从 gmpy2 中挤出一百万位精度?
import gmpy2 # Get the gmpy2 library
print('max_precision =', gmpy2.get_max_precision())
print('emin_min =', gmpy2.get_emin_min())
print('emax_max =', gmpy2.get_emax_max())
gmpy2.get_context().precision = 1000000 + 3 # Set the precision context.
print(gmpy2.get_context())
million_string = 'million digits of pi. I didn't paste it here but you can get it from Eve Andersson's website here, http://www.eveandersson.com/pi/digits/1000000, you'll need to strip out the newlines'
print('string =', million_string)
million_pi = gmpy2.mpfr(million_string)
print('\n\nHere comes the mpfr...\n\n')
print(million_pi)
在我的输出中,我得到了打印的最大值、最小值、上下文、百万位 pi 字符串和大约 pi 的前 300,000 个(给或取)位。我很想准确地向您展示输出,但系统指责我格式精美的输出是代码,并且不让我这样做。我想不出办法。