aleaxit / gmpy

General Multi-Precision arithmetic for Python 2.6+/3+ (GMP, MPIR, MPFR, MPC)
https://gmpy2.readthedocs.io/en/latest/
GNU Lesser General Public License v3.0
513 stars 87 forks source link

gmpy2 does not support trillion-digit Pi calculations. #510

Closed zzy444626905 closed 1 week ago

zzy444626905 commented 1 week ago

code

from gmpy2 import mpz, mpfr, sqrt, get_max_precision
import time
from concurrent.futures import ThreadPoolExecutor
from threading import BoundedSemaphore

print(get_max_precision())

# 测试例子,计算小数点后 10亿 位的圆周率
digits = 10000000000
# 设置 gmpy2 的浮点精度,确保足够的位数
gmpy2.get_context().precision = digits * 4 # 换算成二进制位
# 全局线程池
executor = ThreadPoolExecutor(max_workers=10000)
QABMUL = (640320 ** 3 // 24)
# 信号量,限制队列大小
semaphore = BoundedSemaphore(10000)

output:

D:\miniforge3\envs\yolov8\python.exe C:\Users\zzy\Desktop\python-toolbox\python圆周率计算\gmp超快计算圆周率.py 
2147483391
Traceback (most recent call last):
  File "C:\Users\zzy\Desktop\python-toolbox\python圆周率计算\gmp超快计算圆周率.py", line 15, in <module>
    gmpy2.get_context().precision = digits * 4 # 换算成二进制位
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
ValueError: invalid value for precision
skirpichev commented 1 week ago

Why do you think it's an issue? IIUC, on your system get_max_precision() shows 2147483391. But:

>>> 2147483391 < 10000000000 * 4
True

On my laptop:

Python 3.12.5+ (heads/3.12:0181aa2e3e, Aug 29 2024, 14:55:08) [GCC 12.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from gmpy2 import *
>>> get_max_precision()
9223372036854775551
>>> get_context().precision = 10000000000 * 4  # no error
>>> 

So, please at least provide more information on you system.

zzy444626905 commented 1 week ago

Why do you think it's an issue? IIUC, on your system get_max_precision() shows 2147483391. But:

>>> 2147483391 < 10000000000 * 4
True

On my laptop:

Python 3.12.5+ (heads/3.12:0181aa2e3e, Aug 29 2024, 14:55:08) [GCC 12.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from gmpy2 import *
>>> get_max_precision()
9223372036854775551
>>> get_context().precision = 10000000000 * 4  # no error
>>> 

So, please at least provide more information on you system.

ok

python --version

Python 3.12.3

(yolov8) C:\Users\zzy\Desktop\python-toolbox>python

Python 3.12.3 | packaged by conda-forge | (main, Apr 15 2024, 18:20:11) [MSC v.1938 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> from gmpy2 import *
>>> get_max_precision()
2147483391

(yolov8) C:\Users\zyzhang60\Desktop\python-toolbox>systeminfo

主机名:           zzy
OS 名称:          Microsoft Windows 10 专业版
OS 版本:          10.0.19042 暂缺 Build 19042
OS 制造商:        Microsoft Corporation
OS 配置:          成员工作站
OS 构建类型:      Multiprocessor Free
注册的所有人:     IT
注册的组织:       QXB
产品 ID:          00331-10000-00001-AA596
初始安装日期:     2021/10/25, 10:28:43
系统启动时间:     2024/8/19, 9:03:11
系统制造商:       LENOVO
系统型号:         20S1A0M8CD
系统类型:         x64-based PC

This might not be a bug, it might be an issue with the Python environment. Turns out it's [MSC v.1938 64 bit (AMD64)] on win32. Conda-forge is indeed not as convenient as Conda.

skirpichev commented 1 week ago

Could you please try pip install gmpy2? You miss gmpy2 version, but I would guess it's v2.1.5 (old stable release).

zzy444626905 commented 1 week ago

pip install gmpy2

Could you please try pip install gmpy2? You miss gmpy2 version, but I would guess it's v2.1.5 (old stable release).

pip install gmpy2

(yolov8) C:\Users\zzy\Desktop\python-toolbox>pip install gmpy2
Looking in indexes: https://pypi.tuna.tsinghua.edu.cn/simple/
Requirement already satisfied: gmpy2 in d:\miniforge3\envs\yolov8\lib\site-packages (2.2.0)
skirpichev commented 1 week ago

Looking in indexes: https://pypi.tuna.tsinghua.edu.cn/simple/ Requirement already satisfied: gmpy2 in d:\miniforge3\envs\yolov8\lib\site-packages (2.2.0)

Up to date stable version on PyPI is 2.2.1. Ok, it seems gmpy2 wasn't installed from conda-forge, right?

Anyway, I don't see anything wrong. You are on win32, where long's are 32-bit (ULONG_MAX = 2**32 - 1). So, maximal precision is ((ULONG_MAX -1) >> 1) - 256 == 2147483391. It's not a bug.

zzy444626905 commented 1 week ago

Looking in indexes: https://pypi.tuna.tsinghua.edu.cn/simple/ Requirement already satisfied: gmpy2 in d:\miniforge3\envs\yolov8\lib\site-packages (2.2.0)

Up to date stable version on PyPI is 2.2.1. Ok, it seems gmpy2 wasn't installed from conda-forge, right?

Anyway, I don't see anything wrong. You are on win32, where long's are 32-bit (ULONG_MAX = 2**32 - 1). So, maximal precision is ((ULONG_MAX -1) >> 1) - 256 == 2147483391. It's not a bug.

Alright, it's my mistake, but I still want to thank you.