Repository navigation
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
Open
Rafael-SOWNet wants to merge 1 commit into
Rafael-SOWNet wants to merge 1 commit into
Conversation
…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>
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ 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
Flags with carried forward coverage won't be shown. Click here to find out more.
🚀 New features to boost your workflow:
|
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #2701
Causes, each confirmed by the fix it predicts
tanh zis(e^(2z) - 1)/(e^(2z) + 1).Number.ExpDifferenceevaluatese^(2z) - 1and read both parts against one scale,|e^v| (|e^d| + 1), so the imaginary part ofe^(-200 + 2i) - 1,1.26E-87, which cancels nothing, was read against the 1 and dropped (e^(2(-100+i)) - 1evaluated to-1); the denominator kept it, so the quotient had one of its two contributions. Giving each part its own scale (onlyRe(e^d) - 1cancels) alone madetanh(-100 + i)-1 + 2.5167…E-87iande^(2(-100+i)) - 1keep its part.e^(2z) - 1ande^(2z) + 1both carry the1e-60only in their 61st digit, and the complex quotient's real part(c^2 - 1 + s^2)/|D|^2cancels 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 ofi z:(e^w ∓ 1)/(e^w ± 1)as-i tan(i w/2)/i cot(i w/2),e^w + e^(-w)as2 cos(i w),e^w - e^(-w)as-2i sin(i w)(newHyperbolicSpellingsinFunctions/ExponentialQuotients.cs, hooked intoDivf,SumfandMinusf.DifferenceOfExponentialswhen not exact). A real exponent keeps the spelling. sech(10^(-60) + i) lost its imaginary part the same way ine^z + e^(-z).tan(pi/2 + 10^(-60) i)=-oo + +ooi(true1E+60 i; also wrong on master), fromcos 2x + cosh 2ywithcos 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 bycosh^2 ypast|y| = 4), with ten guard digits within|y| <= 4, and a sine or cosine that is a residual ofxread 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 part1.785E-41(pi's residual overtanh^2(10^-30), above the tolerance of a value near 0). A real tangent whose cosine is a residual is NaN, sotanh(pi/2 i)stays NaN.CtxDividedivides 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 issin 2 / (cosh 200 + cos 2)), tanh is odd sotanh(-100 - i)must be its negation, and the-100side 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/zata + b i,ain±10^-k (k = 20, 40, 60, 80, 99, 120), ±1/3, ±1, ±10, ±100, ±1000,bin±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.The 10 NaN: four poles (
tanh(pi/2 i),coth(pi i),sech(pi/2 i),csch(pi i)), andsech/cschof±10^6 + b iand3 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,sechnear 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%):
ln(1 + x^2)*arctan(x)at 0.37000000000001234 (EvalTranscendentalFresh)sin(x)+cos(x)+tan(x)at 1 (EvalTrig, fresh)tanh(x)at 1+2icosh(x)at 1+2isec(x)at 1+2itan(x)at 1+2i1/xat sqrt(3) 10^12Complex 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,Tan3P2Ifail); with them the pinned real part oftan(3 ± 2i)moved from…418947to 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