https://github.com/python/cpython/commit/5f7d7096129e055dc34838dfc2aee2190c635006
commit: 5f7d7096129e055dc34838dfc2aee2190c635006
branch: main
author: Serhiy Storchaka <[email protected]>
committer: serhiy-storchaka <[email protected]>
date: 2026-09-04T08:10:28Z
summary:

gh-156886: Fix spurious ZeroDivisionError for complex powers (GH-156887)

cos() and sin() of an infinite phase set errno to EDOM, which
complex_pow() reported as a zero base.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>

files:
A 
Misc/NEWS.d/next/Core_and_Builtins/2026-09-03-12-40-00.gh-issue-156886.Kx7mQt.rst
M Lib/test/test_complex.py
M Objects/complexobject.c

diff --git a/Lib/test/test_complex.py b/Lib/test/test_complex.py
index 4cc9eea1f27214..3d02bb6ec2389b 100644
--- a/Lib/test/test_complex.py
+++ b/Lib/test/test_complex.py
@@ -366,6 +366,16 @@ def test_pow(self):
         self.assertRaises(TypeError, pow, None, 1j)
         self.assertAlmostEqual(pow(1j, 0.5), 
0.7071067811865476+0.7071067811865475j)
 
+        # gh-156886: an infinite phase is not a zero base.
+        for base, exp in [(complex(INF), 1j),
+                          (complex(INF, 1), 1j),
+                          (1e300, 1e308j),
+                          (complex(2), complex(0, INF))]:
+            with self.subTest(base=base, exponent=exp):
+                r = base ** exp
+                self.assertTrue(isnan(r.real))
+                self.assertTrue(isnan(r.imag))
+
         a = 3.33+4.43j
         self.assertEqual(a ** 0j, 1)
         self.assertEqual(a ** 0.+0.j, 1)
diff --git 
a/Misc/NEWS.d/next/Core_and_Builtins/2026-09-03-12-40-00.gh-issue-156886.Kx7mQt.rst
 
b/Misc/NEWS.d/next/Core_and_Builtins/2026-09-03-12-40-00.gh-issue-156886.Kx7mQt.rst
new file mode 100644
index 00000000000000..69fbc33992100e
--- /dev/null
+++ 
b/Misc/NEWS.d/next/Core_and_Builtins/2026-09-03-12-40-00.gh-issue-156886.Kx7mQt.rst
@@ -0,0 +1,3 @@
+Fix :exc:`ZeroDivisionError` spuriously raised for a complex power
+with a non-zero base when the phase of the result is infinite,
+e.g. ``1e300**1e308j`` or ``complex('inf')**1j``.
diff --git a/Objects/complexobject.c b/Objects/complexobject.c
index 5f7acdeb7cfd8d..9328baf013c972 100644
--- a/Objects/complexobject.c
+++ b/Objects/complexobject.c
@@ -331,6 +331,9 @@ _Py_c_pow(Py_complex a, Py_complex b)
         r.real = len*cos(phase);
         r.imag = len*sin(phase);
 
+        /* Don't rely on errno set by the math functions above, for
+           example cos() and sin() set EDOM for an infinite phase. */
+        errno = 0;
         if (isfinite(a.real) && isfinite(a.imag)
             && isfinite(b.real) && isfinite(b.imag))
         {

_______________________________________________
Python-checkins mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3//lists/python-checkins.python.org
Member address: [email protected]

Reply via email to