Skip to content

tanh and coth of a large number are their limits - #2305

Merged
Rafael-SOWNet merged 1 commit into
masterfrom
tanh-and-coth-of-a-large-number-are-their-limits
Oct 31, 2026
Merged

Rafael-SOWNet merged 1 commit into
masterfrom
tanh-and-coth-of-a-large-number-are-their-limits

Conversation

@Rafael-SOWNet

Copy link
Copy Markdown
Member

tanh and coth are spelled (e^(2x) - 1) / (e^(2x) + 1) and its reciprocal. Past the decimals' range, or a double's, both sides are infinite and the quotient was NaN.

master and 2.6.0 now
tanh(5568), Evaled and EvalNumerical NaN 1
coth(332640) NaN 1
tanh(+oo) NaN 1, as tanh(-oo) was already -1 and arctan(+oo) is pi / 2
tanh(x) compiled at 400 (FastExpression, LINQ over double and over Complex) NaN 1
  • (b^v + c1) / (b^v + c2) has the same value as 1 + (c1 - c2) / (b^v + c2) wherever it has one, and the second form doesn't divide an overflow by an overflow. ExponentialQuotients writes it that way. Evaluation uses it only where both sides come out infinite. The compilers' pre-pass always writes it so, since a double can't tell an overflow from an infinity.
  • Respelling tanh itself was measured and not taken. As 1 - 2/(e^(2x) + 1), 21 tests change, nearly all integrals the integrator reads in the quotient form.
  • compilesweep gives the same 112 findings on master and here. 7 of the new test's cases fail on master.
  • The suite, the trio, F#, AOT and the eight harnesses pass.

Fixes #2254.

🤖 Generated with Claude Code

tanh and coth are spelled (e^(2x) - 1) / (e^(2x) + 1) and its
reciprocal. Past the range of the decimals both sides are infinite, so
tanh(5568) and coth(332640) evaluated to NaN, where they are 1; past a
double's, tanh(400) compiled to NaN in all three compilers. The
negative side was right only because e^(-11136) is 0.

A quotient of one power plus two constants, (b^v + c1) / (b^v + c2),
is the same value as 1 + (c1 - c2) / (b^v + c2) wherever it has one,
and the second does not divide an overflow by an overflow.
ExponentialQuotients writes it so. Evaluation asks for it where both
sides of a quotient come out infinite, and the compilers' pre-pass
writes every such quotient so, since a double cannot tell an overflow
from an infinity. At +oo it is the limit, 1, as tanh(-oo) was already
-1 and arctan(+oo) is pi/2.

Respelling tanh itself as 1 - 2/(e^(2x) + 1) was measured and not
taken: 21 tests change, nearly all integrals of hyperbolic functions
the integrator reads in the quotient form.

HyperbolicTangentPastTheRangeTest takes both sides evaluated and
compiled, the infinities, and the compiled values inside the range,
near 0 as well, against Math.Tanh. Seven of its cases fail on master.
compilesweep's 112 findings are the same list on master and here. The
suite, the trio, F#, AOT and the eight harnesses pass.

Fixes #2254.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
@Rafael-SOWNet Rafael-SOWNet added this to the 2.7.0 milestone Oct 31, 2026
@Rafael-SOWNet Rafael-SOWNet moved this from Todo to In review in AngouriMath Oct 31, 2026
@codecov

codecov Bot commented Oct 31, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 86.76%. Comparing base (c633c41) to head (7c30af0).
⚠️ Report is 3 commits behind head on master.

Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #2305      +/-   ##
==========================================
+ Coverage   86.74%   86.76%   +0.02%     
==========================================
  Files         310      311       +1     
  Lines       67973    67995      +22     
  Branches    19744    19744              
==========================================
+ Hits        58961    58994      +33     
  Misses       4880     4880              
+ Partials     4132     4121      -11     
Flag Coverage Δ
cpp 37.77% <ø> (ø)
csharp 87.31% <100.00%> (+0.02%) ⬆️
fsharp 32.97% <ø> (ø)
interactive 40.72% <ø> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

Files with missing lines Coverage Δ
...gouriMath/Functions/Compilation/IntoFE/Compiler.cs 85.34% <100.00%> (+0.03%) ⬆️
...Functions/Compilation/IntoLinq/IntoLinqCompiler.cs 97.43% <100.00%> (+0.03%) ⬆️
...nuous/Evaluation.Continuous.Arithmetics.Classes.cs 87.57% <100.00%> (-0.05%) ⬇️
...rces/AngouriMath/Functions/ExponentialQuotients.cs 100.00% <100.00%> (ø)

... and 6 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@Rafael-SOWNet
Rafael-SOWNet merged commit aef5535 into master Oct 31, 2026
36 checks passed
@github-project-automation github-project-automation Bot moved this from In review to Done in AngouriMath Oct 31, 2026
@Rafael-SOWNet
Rafael-SOWNet deleted the tanh-and-coth-of-a-large-number-are-their-limits branch October 31, 2026 21:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

tanh and coth of a large positive number are NaN

1 participant