From bd46aacf5db032e425e66aa1bab7cbb123cce97e Mon Sep 17 00:00:00 2001 From: Sergey B Kirpichev Date: Wed, 21 Jan 2026 04:34:23 +0300 Subject: [PATCH 1/5] Correct mpc.__format__() docstring --- mpmath/ctx_mp_python.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/mpmath/ctx_mp_python.py b/mpmath/ctx_mp_python.py index e231c991..480a9bfc 100644 --- a/mpmath/ctx_mp_python.py +++ b/mpmath/ctx_mp_python.py @@ -753,10 +753,10 @@ class _mpc(mpnumeric): """ ``mpc`` objects allow for formatting similar to Python :external:class:`complex`, specified in :external:ref:`formatspec`. - All of Python's format types are supported, with the exception of - ``'n'``. Also available additional options and formating types, - accepted by :func:`mpmath.mpf.__format__`. + All ``mpf``'s format types and options are supported, with + the exception for ``'%'`` format type, ``'='`` alignment and + zero padding. """ ctx = self.context _, _, (prec, rounding) = self._ctxdata From bfe0ffdb520aedd6acb753d1ae0f4e9df6216bd0 Mon Sep 17 00:00:00 2001 From: Sergey B Kirpichev Date: Thu, 22 Jan 2026 04:32:10 +0300 Subject: [PATCH 2/5] Change behavior of 'b' formatting type to mimic 'a' This behavior is compatible with the gmpy2. Closes #1027 --- mpmath/ctx_mp_python.py | 3 +-- mpmath/libmp/libmpf.py | 8 +------- mpmath/tests/test_format.py | 4 +++- 3 files changed, 5 insertions(+), 10 deletions(-) diff --git a/mpmath/ctx_mp_python.py b/mpmath/ctx_mp_python.py index 480a9bfc..0f742b03 100644 --- a/mpmath/ctx_mp_python.py +++ b/mpmath/ctx_mp_python.py @@ -443,8 +443,7 @@ class _mpf(mpnumeric): >>> f'{x:.15b}' '-1.001111000000110p+0' - Alternate form (``'#'`` option) adds ``0b`` prefix. - + Alternate form (``'#'`` option) works like for ``'a'`` type. """ _, _, (prec, rounding) = self._ctxdata diff --git a/mpmath/libmp/libmpf.py b/mpmath/libmp/libmpf.py index 266ee394..4e652b5f 100644 --- a/mpmath/libmp/libmpf.py +++ b/mpmath/libmp/libmpf.py @@ -1532,10 +1532,6 @@ def format_fixed(s, dps, rnd=round_nearest): return int_part, frac_part -_MAP_FMT_EXP = {'E': 'E', 'e': 'e', 'G': 'E', 'g': 'e', - 'A': 'P', 'a': 'p', 'B': 'p', 'b': 'p', '': 'e'} - - def format_scientific(s, dps, rnd=round_nearest): base = 10 @@ -1672,10 +1668,8 @@ def format_digits(num, format_dict, prec, rnd, _pretty_repr_dps): elif fmt_type == 'b': int_part, frac_part, exponent = format_binary(num, dps, rnd=rnd) - if frac_part: + if frac_part or format_dict['alternate']: frac_part = '.' + frac_part - if format_dict['alternate']: - int_part = '0b' + int_part else: # fixed-point formats int_part, frac_part = format_fixed(num, dps, rnd=rnd) diff --git a/mpmath/tests/test_format.py b/mpmath/tests/test_format.py index 52c3b43d..00ac61f2 100644 --- a/mpmath/tests/test_format.py +++ b/mpmath/tests/test_format.py @@ -902,7 +902,9 @@ def test_binary_fmt(): assert f'{x:b}' == '1.1p+1' assert f'{x:.2b}' == '1.10p+1' assert f'{x:+.2b}' == '+1.10p+1' - assert f'{x:#.2b}' == '0b1.10p+1' + assert f'{x:#.2b}' == '1.10p+1' + assert f'{x:.0b}' == '1p+2' + assert f'{x:#.0b}' == '1.p+2' x = mp.mpf(0) assert f'{x:.2b}' == '0.00p+0' From f3edc81705bf1aac71443c1bde524bef82c3856d Mon Sep 17 00:00:00 2001 From: Sergey B Kirpichev Date: Sun, 1 Feb 2026 04:18:39 +0300 Subject: [PATCH 3/5] Document python-gmp usage --- docs/setup.rst | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/docs/setup.rst b/docs/setup.rst index de7339bd..efaa76fd 100644 --- a/docs/setup.rst +++ b/docs/setup.rst @@ -84,7 +84,7 @@ Python interpreter and do the following:: Using gmpy2 (optional) ---------------------- -If `gmpy2 `_ version 2.2.0 or later is +If `gmpy2 `_ version 2.3.0 or later is installed on your system, mpmath will automatically detect it and transparently use gmpy2 integers instead of Python integers. This makes mpmath much faster, especially at high precision (approximately above 100 digits). @@ -96,6 +96,11 @@ Using the gmpy2 backend can be disabled by setting the ``MPMATH_NOGMPY`` environment variable. Note that the mode cannot be switched during runtime; mpmath must be re-imported for this change to take effect. +Alternatively, you can use `python-gmp +`_ extension. The ``BACKEND`` value +will be equal to 'gmp' in this case. If both extensions are installed on your +system, the gmpy2 will be preferred. + Running tests ------------- From 012368a1d2747aa3a5176b995646855509bbb875 Mon Sep 17 00:00:00 2001 From: Sergey B Kirpichev Date: Wed, 4 Feb 2026 09:53:19 +0300 Subject: [PATCH 4/5] Update gmpy2 deps --- pyproject.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pyproject.toml b/pyproject.toml index bebbc8f0..e4d87e6a 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -35,7 +35,7 @@ Documentation = 'http://mpmath.org/doc/current/' tests = ['pytest>=6', 'numpy', 'packaging', 'pytest-timeout', 'matplotlib', 'pexpect', 'ipython', 'hypothesis'] develop = ['mpmath[tests]', 'flake518>=1.5', 'pytest-cov>=7', 'wheel', 'build'] -gmpy2 = ['gmpy2>=2.3.0a1,<2.3.0a2'] +gmpy2 = ['gmpy2>=2.3'] gmpy = ['mpmath[gmpy2]'] gmp = ['python-gmp>=0.4'] docs = ['sphinx', 'matplotlib', 'sphinxcontrib-autoprogram'] From c35b4d3b6d41a335169bdc5ca4b9fef5191fc0ad Mon Sep 17 00:00:00 2001 From: Sergey B Kirpichev Date: Wed, 4 Feb 2026 15:08:08 +0300 Subject: [PATCH 5/5] Require python-gmp>=0.5 --- pyproject.toml | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/pyproject.toml b/pyproject.toml index e4d87e6a..5867537f 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -37,7 +37,8 @@ tests = ['pytest>=6', 'numpy', 'packaging', 'pytest-timeout', develop = ['mpmath[tests]', 'flake518>=1.5', 'pytest-cov>=7', 'wheel', 'build'] gmpy2 = ['gmpy2>=2.3'] gmpy = ['mpmath[gmpy2]'] -gmp = ['python-gmp>=0.4'] +gmp = ['python-gmp>=0.5; python_version>="3.11"', + 'python-gmp>=0.4; python_version<"3.11"'] docs = ['sphinx', 'matplotlib', 'sphinxcontrib-autoprogram'] ci = ['pytest-xdist', 'diff_cover']