Repository navigation
Read a sine or cosine factor that is a residual of its argument as 0 at any size - #2703
Merged
Merged
Conversation
…at any size A part of cos(x + i y), sin(x + i y) or e^(y + i x) is the sine or the cosine of x times cosh y, sinh y or e^y. Where that factor is 0 for the exact x, the hundred digits leave about 1e-100 of it, and the product was read as 0 only while it was below the downcasting tolerance itself: Real.CreateAtScale reaches its residual test only for a value near 0, while the imaginary part was read against its scale at any size. From |y| near 116 a real part that is 0 came back as that residual times cosh y: cos(pi/2 + 1000 i) had a real part of -8.8E+332. The factor is now read as on the real line, against the size of x, as past the exponents already was (#2699): a part whose factor is a residual is 0. ExpDifference takes its two powers through Exp with that reading while the downcasting is off inside it, so sinh follows, and tanh's imaginary part, which was e^(2 z)'s residual, is right. A factor that is small but not 0 keeps its part. Fixes #2700 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 #2703 +/- ##
========================================
Coverage 86.62% 86.62%
========================================
Files 346 346
Lines 76782 76884 +102
Branches 22566 22611 +45
========================================
+ Hits 66509 66599 +90
- Misses 5546 5548 +2
- Partials 4727 4737 +10
Flags with carried forward coverage won't be shown. Click here to find out more.
🚀 New features to boost your workflow:
|
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 #2700
Cause
A part of
cos(x + i y),sin(x + i y)ore^(y + i x)issin xorcos xtimescosh y,sinh yore^y, passed toComplex.CreateAtScaleswith the scalereScale * cosh y. The two parts were read differently.Complex.Createdrops an imaginary part belowtolerance * scaleat any size. A real part goes throughReal.CreateAtScale, which reaches its residual test (IsAResidual) only for a value thatMayBeASmallRationaland splits with whole part 0, that is, only within the tolerance (1e-50) of 0 in absolute terms. Socos(pi/2)'s~1e-100residual timescosh ywas dropped while the product stayed below1e-50(|y| < ~116). Above that it came back as a number.cos(pi/2 + 120 i)already had a real part of-5.8E-50.I tested the prediction before naming the cause.
sin(pi/2 + 1000 i)andcos(pi + 1000 i), whose residual is in the imaginary part, were already right on master. Zeroing the factor fixed every real-part row in the sweep.Fix
The rule from #2699's
TimesAHyperbolicPastTheExponentsnow covers the finite range too. A factor below the tolerance timesPeriodicResidualScale(x)(the real-line scale:|x|up to 1,sqrt|x|above for a decimal, 1 for an exact rational) is 0, and so is its part. That decision is made on the factor (sin x/cos x) against the size of x, never on the part against the modulus. So:cos(1/10^40 + 1000 i):sin(1e-40)against1e-40is not a residual, and the imaginary part keeps its digits.exp(1000 + 10^-80 i):sin(1e-80)against1e-80, the same.sinhgoes throughExpDifference, which runsExpwith the downcasting off. It now passes the caller's downcasting setting intoExpso thatExpreads the factors the same way. A first version applied a relative rule toExpDifference's real part instead. The sweep caught that it zeroed the genuine-1ofe^(2z) - 1and halvedtanh(100 + pi/4 i)'s imaginary part, so it was dropped. The bound is decided from the decimal exponents first, and is made exactly only within a digit of it. Made exactly every time, it cost about 3% bytes oncos(1 + 2 i).One deliberate consequence: a factor within the tolerance of 0 relative to
xis now 0 at everyy. That is how the real line treats it (cos(pi/2 + 10^-60)is0), how master already treated it for smally, and how #2699 treats it past the exponents.cos(pi/2 + 10^-60 + 100 i)had a real part of-1.34E-17and now has none.cos(pi/2 + 10^-60 + 10 i)already had none on master.Measurements
Sweep against mpmath (450 digits, with a part counted as exactly 0 where it disagrees with itself at 1000 digits). It covers 8400 values of cos, sin, tan, sec, csc, cot, cosh, sinh, tanh and exp:
a ± i yand± y + a i;aink pi/2(k = -4..4),±10^-40,±10^-80,±1,±1/3,±pi/3,±pi/4;yin 10, 100, 120, 230, 231, 300, 1000, 10^4, 10^5, 2·10^5.Master is 28bec0a.
Every changed row:
tanh(y ± pi/4 i)imaginary parts → right. They weree^(2z)'s residual, e.g.tanh(10^5 + pi/4 i)was1 + 3.49E-108 iwhere it is1 + 2.54E-86859 i.tanh(2·10^5 + k pi/2 i)NaN →1.sinh(10^4 ± pi/4 i), right on both: the 100th digit moved (…832 / …833 swapped between the parts).No other row that was right on master changed.
Not changed, and pre-existing on master:
tanh(-100 + a i)(12 rows) has an imaginary part half of its true~1e-87value. That is a separate defect in the quotient(e^(2z) - 1)/(e^(2z) + 1)near-1. 160 genuine parts below1e-45of the modulus (cosh, tanh) are dropped by the existing half-digit convention.Allocation:
GC.GetAllocatedBytesForCurrentThread, 3000 ops after 300 warm-up, minimum of 2 runs. Master is 82eb869, which differs from 28bec0a only in #2699's past-the-exponents path, which none of these reach.ln(1 + x^2)*arctan(x)sin(x)cos(x)tan(x)sin(x)cos(x)tan(x)tan(x)cot(x)sec(x)csc(x)cos(x)exp(x)exp(x)sinh(x)cosh(x)The gated
EvalTranscendentalFreshpath and real sin/cos/tan/exp are unchanged to the byte. Complex sin/cos/exp/sec/csc/sinh/cosh are within +0.6%.Suites (net10.0, Release):
limitcheck / symlimitcheck: built against this branch and against 28bec0a, run alternating twice each under the lock. The reports are byte-identical. Times:
Tests:
Issue2700_APartThatIsZeroForTheExactArgumentIsZeroAtAnySizeandIssue2700_ASmallPartThatIsNotZeroKeepsItsDigits, with values from mpmath at 130 digits.BREAKING-CHANGES.mdhas an entry under its own heading after the #2698 one.🤖 Generated with Claude Code