Skip to content

Avoid NaN real components in complex tanh after overflow - #1435

Closed
agourakis82 wants to merge 1 commit into
xtensor-stack:masterfrom
agourakis82:fix/complex-tanh-overflow-1432
Closed

agourakis82 wants to merge 1 commit into
xtensor-stack:masterfrom
agourakis82:fix/complex-tanh-overflow-1432

Conversation

@agourakis82

@agourakis82 agourakis82 commented Oct 27, 2026 •

Copy link
Copy Markdown
Contributor

When cosh(2*real(z)) overflows, the real part of complex tanh was an inf/inf quotient and became NaN. The non-fast-math path now substitutes the sign of the real input once the denominator is infinite. An 8 by 8 grid compares the batch with std::tanh, covering huge real parts, signed zeros and ordinary controls. That grid is omitted under fast-math, which does not implement the recovery.

Part of #1432.

Written by me with AI assistance.

@agourakis82
agourakis82 marked this pull request as ready for review October 28, 2026 09:10
@agourakis82

Copy link
Copy Markdown
Contributor Author

Published in 1b61a71.

I moved the overflow regression into the existing typed tanh subcase and added finite near-saturation inputs that execute with and without fast-math. The overflow and largest-value inputs retain a narrow strict-mode guard because the implementation's infinity correction is itself guarded in fast-math. The finite comparisons use explicit numerical assertions in both modes.

On macOS ARM64 with Apple Clang 21 and C++23, the scoped complex and real hyperbolic tests pass all four cases in each mode, with 128 assertions in strict mode and 28 in fast-math. Removing the overflow correction in an isolated header copy causes 40 assertions to fail in strict mode. A separate numerator mutation is rejected in both modes, confirming that the shared finite numerical checks execute.

Prepared with OpenAI Codex assistance.

When cosh(2*real(z)) overflows, use the sign of the real part instead of an inf/inf quotient.
@DiamonDinoia

Copy link
Copy Markdown
Contributor

Superseded by #1456

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.

2 participants