python / python/cpython

Spurious ZeroDivisionError for complex powers when the phase is infinite

Offen
#156,886 1 Kommentar 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

interpreter-core type-bug
Vorherrschende Sprache
Python
Sterne
77.2k
Forks
36k
Ø Merge
1 T. 9 Std.
Gemergte PRs (30 T.)
558

Beschreibung

>>> complex('inf')**1j
Traceback (most recent call last):
  File "<python-input-0>", line 1, in <module>
    complex('inf')**1j
    ~~~~~~~~~~~~~~^^~~
ZeroDivisionError: zero to a negative or complex power

The base is not zero. The result should be nan+nanj, as in the GNU MPC.

_Py_c_pow() computes an infinite phase here, and cos() and sin() of an infinite argument are domain errors, so libm sets errno to EDOM. complex_pow() translates any EDOM to ZeroDivisionError, as it cannot distinguish it from the EDOM set for a zero base.

Any finite base for which the phase overflows is affected too:

>>> complex(1e300)**complex(0, 1e308)
ZeroDivisionError: zero to a negative or complex power
>>> complex(2)**complex(0, float('inf'))
ZeroDivisionError: zero to a negative or complex power

sin() and cos() set errno only for infinite arguments, so it is enough to discard errno in _Py_c_pow() after computing the result: _Py_ADJUST_ERANGE2() recovers ERANGE from the result itself.

Noticed by @hpkfft in #156694.

Linked PRs
  • gh-156887

Beitragsleitfaden

Beitragsleitfaden öffnen

Erste Schritte

  1. Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
  3. Forke das Repository und arbeite in einem Branch.
  4. Öffne einen Pull Request, der die Issue-Nummer nennt.

Rechercherichtung

Beginne bei _Py_c_pow() und complex_pow() und verwende die Reproduktionen im Issue, um das errno-Handling für unendliche Phasen zu beobachten. Überprüfe, dass das Ergebnis nan+nanj statt ZeroDivisionError ist, und führe die relevanten Tests für komplexe Potenzen aus, nachdem du den Status des verknüpften PR bestätigt hast.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
python
Bereich
backend
Issue-Typ
Bug
Schwierigkeit
2/5
Geschätzter Aufwand
1-3 Stunden
Aktivitätsstatus
Veraltet
Klarheit
Klar beschrieben
Anfängerfreundlichkeit
25/100

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.