Skip to content

Preserve branch-cut signs and special values in complex sqrt - #95

Open
jmh530 wants to merge 1 commit into
libmir:masterfrom
jmh530:fix-complex-sqrt
Open

jmh530 wants to merge 1 commit into
libmir:masterfrom
jmh530:fix-complex-sqrt

Conversation

@jmh530

@jmh530 jmh530 commented Oct 14, 2026

Copy link
Copy Markdown
Contributor

Correct complex square roots at signed-zero branch cuts and for
nonfinite inputs. For example, sqrt(-4 - 0i) now returns +0 - 2i,
and sqrt(1 + ∞i) returns ∞ + ∞i instead of ∞ + NaNi.

Preserve the imaginary sign of zero and use copysign when constructing
roots with negative real inputs. Handle infinities and NaNs explicitly
while retaining the scaled formula for ordinary finite inputs.

Document the principal-root convention and add runtime and CTFE
regressions across float, double, and real. Tests cover branch-cut
signs, nonfinite values, extreme finite magnitudes, and ordinary
numerical accuracy.

Validation:

  • Windows DMD and LDC unit suites: 19 modules passed on each.
  • Focused optimized sqrt checks passed on Windows DMD/LDC and Linux LDC.
  • Whitespace checks passed.

The regressions retain the existing mir_core_test guard convention.
They ran locally, but the current CI coverage configuration omits that
version identifier. PR #94 contains the configuration fix needed for
CI to run these guarded tests. The sqrt implementation is otherwise
independent of that PR.

AI-Assisted-By: Codex (GPT-6 Astra Medium)

Complex sqrt lost the imaginary sign of zero and selected the wrong
side of the negative-real branch cut for inputs such as (-4, -0).
Infinite imaginary components also entered the finite calculation,
producing NaN instead of an infinite imaginary result.

Preserve the input imaginary zero and use copysign when constructing
roots with negative real inputs. Handle infinities and NaNs explicitly
before the finite calculation, while retaining the scaled formula
for ordinary inputs.

Document the principal-root convention and add runtime and CTFE
regressions across float, double, and real, covering branch-cut signs,
nonfinite values, and extreme finite magnitudes.

AI-Assisted-By: Codex (GPT-6 Astra Medium)
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