Skip to content

Keep the small parts of tanh, coth, sech and csch off the real line, and every digit of a small quotient - #2704

Open
Rafael-SOWNet wants to merge 1 commit into
masterfrom
fix-2701-small-parts-of-tanh-and-reciprocals
Open

Rafael-SOWNet wants to merge 1 commit into
masterfrom
fix-2701-small-parts-of-tanh-and-reciprocals

Conversation

@Rafael-SOWNet

Copy link
Copy Markdown
Member

Fixes #2701

Causes, each confirmed by the fix it predicts

  1. tanh(-100 + i) had half its imaginary part. tanh z is (e^(2z) - 1)/(e^(2z) + 1). Number.ExpDifference evaluates e^(2z) - 1 and read both parts against one scale, |e^v| (|e^d| + 1), so the imaginary part of e^(-200 + 2i) - 1, 1.26E-87, which cancels nothing, was read against the 1 and dropped (e^(2(-100+i)) - 1 evaluated to -1); the denominator kept it, so the quotient had one of its two contributions. Giving each part its own scale (only Re(e^d) - 1 cancels) alone made tanh(-100 + i) -1 + 2.5167…E-87i and e^(2(-100+i)) - 1 keep its part.
  2. tanh(10^(-60) + i) and coth lost a real part of size 1e-60. Measured: e^(2z) - 1 and e^(2z) + 1 both carry the 1e-60 only in their 61st digit, and the complex quotient's real part (c^2 - 1 + s^2)/|D|^2 cancels sixty digits and is read as a residual. No reading of the scales can recover digits the powers never had, so off the real line the parser's spellings are now evaluated through the trigonometric function of i z: (e^w ∓ 1)/(e^w ± 1) as -i tan(i w/2) / i cot(i w/2), e^w + e^(-w) as 2 cos(i w), e^w - e^(-w) as -2i sin(i w) (new HyperbolicSpellings in Functions/ExponentialQuotients.cs, hooked into Divf, Sumf and Minusf.DifferenceOfExponentials when not exact). A real exponent keeps the spelling. sech(10^(-60) + i) lost its imaginary part the same way in e^z + e^(-z).
  3. tan near a pole was a residual of its own denominator. Routing tanh through tan exposed tan(pi/2 + 10^(-60) i) = -oo + +ooi (true 1E+60 i; also wrong on master), from cos 2x + cosh 2y with cos 2x = -1. Tan off the real line is now (sin x cos x + i sinh y cosh y)/(cos^2 x + sinh^2 y) (divided by cosh^2 y past |y| = 4), with ten guard digits within |y| <= 4, and a sine or cosine that is a residual of x read as 0, as Read a sine or cosine factor that is a residual of its argument as 0 at any size #2703 does for sin and cos. Cot reads its factors the same way: cot(pi + 10^(-30) i) had the real part 1.785E-41 (pi's residual over tanh^2(10^-30), above the tolerance of a value near 0). A real tangent whose cosine is a residual is NaN, so tanh(pi/2 i) stays NaN.
  4. sec(1/2 - 30i) kept 88 digits. CtxDivide divides to 100 decimal places and redivides only below (p - 5) * 3 = 285 bits, about 86 digits, so any quotient between ~1e-14 and 0.1 kept 86–99 digits (1/(sqrt(3) 10^12) too, not only complex reciprocals). A quotient surely below 0.1 (estimated from both sides' exponents and mantissa bits) is now divided to as many more places as it has zeros after the point and rounded to the digits; the second-division threshold is all but one digit.

tanh(100 + i): it now keeps its imaginary part 2.5167…E-87, below 1e-50 of its modulus. I chose to keep it: the part is no residual of anything (it is sin 2 / (cosh 200 + cos 2)), tanh is odd so tanh(-100 - i) must be its negation, and the -100 side keeps it. The 1e-50 convention applies to parts made by cancellation, which this is not.

Measurements

Sweep against mpmath (450 digits, zero parts checked at 1000): tanh, coth, sech, csch, tan, cot, sec, csc, 1/z at a + b i, a in ±10^-k (k = 20, 40, 60, 80, 99, 120), ±1/3, ±1, ±10, ±100, ±1000, b in ±1, ±1/3, ±pi/4, ±10^-k, plus 26 ordinary/large/pole arguments: 3798 rows. Parts held to 1e-95 relative; a zero part must be exactly 0.

master (0ce20c5) branch
right 3040 3716
wrong value / dropped part > 1e-45 / residual kept 39 0
part with fewer than 85 digits 96 0
fewer digits (85–95, or a small part inexact) 218 0
small part dropped 289 0
a part snapped to an integer by the downcasting, other part ~80 digits 100 72
NaN 16 10

The 10 NaN: four poles (tanh(pi/2 i), coth(pi i), sech(pi/2 i), csch(pi i)), and sech/csch of ±10^6 + b i and 3 10^5 + 2i, which are below the exponents (NaN on master too). The 72 snapped rows are the downcasting convention (a part within 1e-50 of an integer becomes it); 16 of them, sech near 0, had the other part dropped on master. 1574 values changed; no part that was right on master moved by more than 10x its master error or past 1e-98 relative; largest branch error on a changed non-snapped part is 2.0e-99 (csch). #2700's 8400-row sweep: master 12 wrong + 12 NaN (tanh(-100 + b i), tanh(2 10^5 + b i)), branch 8400 right.

Allocation (bytes/op, 3000 ops after warm-up, master 0ce20c5 vs branch, three alternating runs, all identical within ±0.3%):

path master branch
ln(1 + x^2)*arctan(x) at 0.37000000000001234 (EvalTranscendentalFresh) 34544 34544 0%
sin(x)+cos(x)+tan(x) at 1 (EvalTrig, fresh) 69630 69774 +0.2%
same at 0.37 73773 73979 +0.3%
tanh(x) at 1+2i 142473 85019 -40%
cosh(x) at 1+2i 139028 107956 -22%
sec(x) at 1+2i 74392 75171 +1.0%
tan(x) at 1+2i 60008 66646 +11% (guard digits)
1/x at sqrt(3) 10^12 3040 4493 +48% (the extra places)

Complex tan is about 45% slower (31 → 45 us): the guard digits. Without them its parts are 2–14 units off in the last digit (NumericDigits.TanI, Tan3P2I fail); with them the pinned real part of tan(3 ± 2i) moved from …418947 to the correctly rounded …418949 (true …4189487).

limitcheck and symlimitcheck, master vs branch alternating under the flock: identical reports; limitcheck 31.7 / 29.3 s master vs 30.4 / 28.9 s branch, symlimitcheck 26.5 / 26.1 s vs 27.1 / 25.0 s.

Suites: C# 22309 passed, 0 failed, 13 skipped (574 s); F# 135 passed.

New tests: Issue2701_ASmallPartOfAHyperbolicFunctionOrAReciprocalKeepsItsDigits (12 rows), Issue2701_ASmallQuotientKeepsEveryDigit, Issue2701_APoleOfTanhOffTheRealLineHasNoValue. BREAKING-CHANGES entry after #2700's.

🤖 Generated with Claude Code

…and every digit of a small quotient

tanh and coth are spelled (e^(2 z) - 1)/(e^(2 z) + 1) and its reciprocal, and sech and csch as reciprocals of
(e^z +- e^(-z))/2. Evaluated as written, a part small beside the two powers cancelled in the quotient or the
sum: tanh(10^(-60) + i) lost its real part 3.4E-60, coth likewise, and sech(10^(-60) + i) its imaginary part.
Off the real line each spelling is now evaluated as the trigonometric function of i z: tanh z = -i tan(i z),
coth z = i cot(i z), cosh z = cos(i z), sinh z = -i sin(i z), whose parts cancel nothing but a residual of a
multiple of pi/2. A real exponent keeps the spelling.

e^u - 1 read both parts against one scale, |e^v| (|e^d| + 1), so the imaginary part of e^(-200 + 2 i) - 1,
which cancels nothing, was read against the 1 and dropped; tanh(-100 + i) takes it once from each side of its
quotient and had half its imaginary part. Each part is now read against what it is a sum of.

tan off the real line took its denominator as cos 2x + cosh 2y, itself a residual near a pole, so
tan(pi/2 + 10^(-60) i), 10^60 i, was -oo + oo i; it is now cos^2 x + sinh^2 y, and tan and cot read a sine or a
cosine that is a residual of x as 0, as sin and cos do: the real part of cot(pi + 10^(-30) i) was 1.8E-41. A
real tangent whose cosine is a residual is NaN, as at an exact pole.

A quotient was divided to a hundred decimal places and divided again to digits only below 285 bits, so one
below 0.1 kept as few as 86 digits; sec(1/2 - 30 i), the reciprocal of a cosine near 5e12, kept 88. One that
is surely below 0.1, from the two sides' exponents and bits, is now divided to as many more places as it has
zeros after the point and rounded to the digits, and the second division is made below all but one of the
working digits. tan within |y| <= 4 is taken with ten guard digits, and the pinned last digit of the real
part of tan(3 +- 2 i) is now the correctly rounded one.

tanh(100 + i) keeps its imaginary part 2.5E-87, below 1e-50 of its modulus: it is no residual, and
tanh(-100 + i) keeps it.

Fixes #2701

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@Rafael-SOWNet Rafael-SOWNet added this to the 2.11 milestone Dec 4, 2026
@codecov

codecov Bot commented Dec 4, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 86.62%. Comparing base (0ce20c5) to head (0e9fc53).

Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #2704      +/-   ##
==========================================
- Coverage   86.62%   86.62%   -0.01%     
==========================================
  Files         346      346              
  Lines       76884    76863      -21     
  Branches    22611    22608       -3     
==========================================
- Hits        66603    66584      -19     
  Misses       5546     5546              
+ Partials     4735     4733       -2     
Flag Coverage Δ
cpp 37.77% <ø> (ø)
fsharp 32.79% <ø> (ø)
interactive 40.09% <ø> (ø)

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

Files with missing lines Coverage Δ
...uriMath/Core/Entity/Continuous/Number/Operators.cs 90.64% <ø> (+<0.01%) ⬆️
...nuous/Evaluation.Continuous.Arithmetics.Classes.cs 87.11% <ø> (ø)
...rces/AngouriMath/Functions/ExponentialQuotients.cs 100.00% <ø> (ø)
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

This branch has not been deployed

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

Labels

None yet

Projects

Status: Todo

Development

Successfully merging this pull request may close these issues.

tanh and coth drop a small real part, and sec and csc of a complex argument keep 88 digits

1 participant