FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

gh-156886: Fix spurious ZeroDivisionError for complex powers by serhiy-storchaka · Pull Request #156887 · python/cpython · GitHub

Repository navigation

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .c  (1) .py  (1) .rst  (1) All 3 file types selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
10 changes: 10 additions & 0 deletions Lib/test/test_complex.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
@@ -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``.
3 changes: 3 additions & 0 deletions Objects/complexobject.c
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -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))
{
Expand Down
Loading

Back | FazBrowse Home | New Git URL