Skip to content

Avoid intermediate overflow and underflow in complex abs - #583

Open
CAOShurong wants to merge 1 commit into
pydata:masterfrom
CAOShurong:codex/fix-complex-absolute-range
Open

CAOShurong wants to merge 1 commit into
pydata:masterfrom
CAOShurong:codex/fix-complex-absolute-range

Conversation

@CAOShurong

Copy link
Copy Markdown

Complex abs() squares both components before taking the square root. Without VML, abs(3e200 + 4e200j) returns infinity and abs(3e-200 + 4e-200j) returns zero, although their magnitudes are representable. This also breaks normalization in sign().

Use hypot(), already used by the complex logarithm and square-root helpers. Add regressions for large/small values, strided and in-place evaluation, and nonfinite inputs; retain the complex result type.

Validated on Windows/MSVC, Python 3.13 and NumPy 2.3.5 without VML: all 6,045 tests pass, including from the built wheel. The three new tests fail on the baseline. In 12 interleaved single-thread measurements on ordinary complex values, abs() took about 1.26x and sign() 1.22x the baseline time; this change trades that overhead for correct range handling.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant