我正在编写一个Python3(64位)程序,使用64位Windows 10上的gmpy2模块计算pi至少100万位.我正在使用Chudnovsky算法.系列算法是天才,易于跟踪和直接实现.我遇到的问题是用gmpy2表示非常大的精确实数.
我把这个问题归结为一个非常简单的测试.
- 检查gmpy2系统的限制。它们看起来很棒。
- 按照百万位数pi的需要设置精确的上下文。
- 分配一个带有一百万个数字pi的字符串变量。
- 使用mpfr(‘百万位数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('
Here comes the mpfr...
')
print(million_pi)
在我的输出中,我得到了打印的最大值,管理器,上下文,百万位pi字符串和大约前300,000(给或带)位数.我想正确显示输出,但系统将我的格式化良好的输出指为代码,不会让我这样做.我无法弄清楚解决这个问题的方法.