Skip to content

Improve complex exp and log handling at numerical boundaries - #97

Open
jmh530 wants to merge 2 commits into
libmir:masterfrom
jmh530:codex/fix-complex-exp-axis
Open

jmh530 wants to merge 2 commits into
libmir:masterfrom
jmh530:codex/fix-complex-exp-axis

Conversation

@jmh530

@jmh530 jmh530 commented Oct 15, 2026

Copy link
Copy Markdown
Contributor

Fix incorrect results in complex exp on the real axis and improve the range and accuracy of complex log.

For exp, preserve the imaginary signed zero when the input lies on the real axis. Return the scalar exponential directly, avoiding a NaN imaginary component when a finite real exponential overflows.

For log, compute the logarithmic magnitude without first forming a magnitude that can overflow or lose subnormal precision. Near unit magnitude, compensate for rounding in the squared norm so cancellation does not erase small results or reverse their sign. Use log1p at runtime and a bounded series during compile-time evaluation.

The changes are split into separate exp and log commits. Regression tests retain the existing mir_core_test guards and cover float, double, and real at runtime and compile time.

Validation:

  • The 19-module test runner passed with Windows DMD 2.113.0 and LDC 1.43.
  • Optimized DMD complex-module tests and focused optimized LDC regressions on Windows and Linux passed.
  • Focused optimized 32-bit Windows regressions passed with both DMD and LDC.
  • A 999-case near-unit comparison against high-precision references found no wrong-sign results; maximum absolute error was approximately 1.2e-32.
  • Broader optimized LDC runs encounter existing assertions in complex log on Windows and complex exp on Linux, reproduced on unchanged upstream.
  • Final review of the combined branch found no blocking issues.

Upstream's current coverage build does not enable mir_core_test, so it does not execute these guarded regressions. Local regression testing explicitly enabled them. The coverage-build correction is proposed separately in #94, and macOS CI fixes are proposed in #96.

Extended-real compile-time range and precision remain limited by the underlying scalar log implementation in some configurations. This is tracked separately in dlang/phobos#11118.

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

Handle zero imaginary components before the special-case and polar
paths, returning the scalar exponential with the original imaginary
zero. This preserves negative zero for zero and infinite real inputs
and avoids a NaN imaginary component when a finite real exponential
overflows.

Remove the redundant imaginary-zero branches and cover signed zeros,
infinities, NaN, and finite overflow and underflow at runtime and
compile time for float, double, and real.

AI-Assisted-By: Codex (GPT-6 Astra Medium)
Compute the logarithmic magnitude without forming a potentially
overflowing or poorly rounded magnitude. Preserve small results near
unit magnitude by accounting for cancellation in the squared norm.

Use log1p at runtime and a bounded series during compile-time
evaluation. Preserve the existing special-value and branch-cut
handling.

Add guarded regressions for large and subnormal inputs, near-unit
cancellation, and calculation boundaries across floating-point types.

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