gmpy2 mpfr 值限制为 301,033 位,其中 301,032 位是正确的

gmpy2 mpfr values are limited to 301,033 digits of which the 301,032 digits are correct

我正在编写一个 Python3(64 位)程序,使用 64 位上的 gmpy2 模块将 pi 计算到至少一百万位 Windows10。我正在使用 Chudnovsky算法。级数算法是天才的,易于遵循且易于实施。我遇到的问题是用 gmpy2 表示非常大的精度实数。

我把这个问题归结为一个非常简单的测试。

  1. 检查 gmpy2 系统限制。他们看起来很棒。
  2. 根据需要为百万位 pi 设置精度上下文。
  3. 分配一个带有百万位 pi 的字符串变量。
  4. 使用 mpfr('million digit pi')
  5. 为字符串转换分配一个 mpfr 变量
  6. 打印字符串变量,它有一百万位。
  7. 打印 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 字符串和大约前 300,000(给或取)pi 数字。我很想向您展示准确的输出,但系统指责我的格式良好的输出是代码,不让我这样做。我想不出解决办法。

精度以位而不是十进制数字指定。每个十进制数字需要 log(10)/log(2) 位。 1,000,003 位将产生 1000003*log(2)/log(10) 或大约 301,030.9 个精确的十进制数字。