Skip to content

Read a sine or cosine factor that is a residual of its argument as 0 at any size - #2703

Merged
Rafael-SOWNet merged 1 commit into
masterfrom
fix-2700-a-residual-at-a-large-modulus
Dec 4, 2026
Merged

Rafael-SOWNet merged 1 commit into
masterfrom
fix-2700-a-residual-at-a-large-modulus

Conversation

@Rafael-SOWNet

Copy link
Copy Markdown
Member

Fixes #2700

Cause

A part of cos(x + i y), sin(x + i y) or e^(y + i x) is sin x or cos x times cosh y, sinh y or e^y, passed to Complex.CreateAtScales with the scale reScale * cosh y. The two parts were read differently. Complex.Create drops an imaginary part below tolerance * scale at any size. A real part goes through Real.CreateAtScale, which reaches its residual test (IsAResidual) only for a value that MayBeASmallRational and splits with whole part 0, that is, only within the tolerance (1e-50) of 0 in absolute terms. So cos(pi/2)'s ~1e-100 residual times cosh y was dropped while the product stayed below 1e-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) and cos(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 TimesAHyperbolicPastTheExponents now covers the finite range too. A factor below the tolerance times PeriodicResidualScale(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) against 1e-40 is not a residual, and the imaginary part keeps its digits.
  • exp(1000 + 10^-80 i): sin(1e-80) against 1e-80, the same.

sinh goes through ExpDifference, which runs Exp with the downcasting off. It now passes the caller's downcasting setting into Exp so that Exp reads the factors the same way. A first version applied a relative rule to ExpDifference's real part instead. The sweep caught that it zeroed the genuine -1 of e^(2z) - 1 and halved tanh(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 on cos(1 + 2 i).

One deliberate consequence: a factor within the tolerance of 0 relative to x is now 0 at every y. That is how the real line treats it (cos(pi/2 + 10^-60) is 0), how master already treated it for small y, and how #2699 treats it past the exponents. cos(pi/2 + 10^-60 + 100 i) had a real part of -1.34E-17 and 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:

  • arguments a ± i y and ± y + a i;
  • a in k pi/2 (k = -4..4), ±10^-40, ±10^-80, ±1, ±1/3, ±pi/3, ±pi/4;
  • y in 10, 100, 120, 230, 231, 300, 1000, 10^4, 10^5, 2·10^5.

Master is 28bec0a.

master branch
right 7602 8042
a part that is 0 written as a number 416 (cos, sin, exp, sec, csc, cosh, sinh, 64/64/32/64/64/64/64) 0
imaginary part of tanh right only to the modulus 190 174
NaN 20 12
WRONG 12 12 (the same rows, unchanged)

Every changed row:

  • 416 residual rows → right.
  • 16 tanh(y ± pi/4 i) imaginary parts → right. They were e^(2z)'s residual, e.g. tanh(10^5 + pi/4 i) was 1 + 3.49E-108 i where it is 1 + 2.54E-86859 i.
  • 8 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-87 value. That is a separate defect in the quotient (e^(2z) - 1)/(e^(2z) + 1) near -1. 160 genuine parts below 1e-45 of 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.

expression at master B/op branch B/op delta master us branch us
ln(1 + x^2)*arctan(x) 0.37000000000001234 34680.0 34680.0 +0.00% 52.8 53.1
sin(x) 0.37 21992.0 21992.0 +0.00% 31.5 31.1
cos(x) 0.37 21992.0 21992.0 +0.00% 31.3 31.0
tan(x) 0.37 24896.0 24896.0 +0.00% 41.0 40.7
sin(x) 1 + 2 * i 51616.0 51872.0 +0.50% 84.5 86.1
cos(x) 1 + 2 * i 51904.0 52160.0 +0.49% 54.5 44.7
tan(x) 1 + 2 * i 60497.7 60520.7 +0.04% 45.3 43.5
tan(x) 1 + 10 * i 91792.0 92018.6 +0.25% 62.8 66.7
cot(x) 1 + 2 * i 80104.0 80104.0 +0.00% 51.1 51.6
sec(x) 1 + 2 * i 74528.0 74784.0 +0.34% 45.4 46.8
csc(x) 1 + 2 * i 73584.0 73840.0 +0.35% 40.7 41.1
cos(x) 1/3 + 300 * i 61808.0 62032.0 +0.36% 33.7 33.6
exp(x) 0.37 14984.0 14984.0 +0.00% 8.5 8.4
exp(x) 1 + 2 * i 43728.0 43984.0 +0.59% 23.8 24.1
sinh(x) 1 + 2 * i 164270.4 164947.7 +0.41% 99.6 103.1
cosh(x) 1 + 2 * i 138416.0 138959.2 +0.39% 67.9 70.9

The gated EvalTranscendentalFresh path 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):

  • C#: 22266 passed, 13 skipped, 0 failed (8 m 28 s).
  • F#: 135 passed.

limitcheck / symlimitcheck: built against this branch and against 28bec0a, run alternating twice each under the lock. The reports are byte-identical. Times:

  • limitcheck: 28.3/27.4 s master, 28.5/28.3 s branch.
  • symlimitcheck: 25.2/25.2 s master, 25.2/25.2 s branch.

Tests: Issue2700_APartThatIsZeroForTheExactArgumentIsZeroAtAnySize and Issue2700_ASmallPartThatIsNotZeroKeepsItsDigits, with values from mpmath at 130 digits. BREAKING-CHANGES.md has an entry under its own heading after the #2698 one.

🤖 Generated with Claude Code

…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>
@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 (28bec0a) to head (e0aa410).
⚠️ Report is 1 commits behind head on master.

Additional details and impacted files

Impacted file tree graph

@@           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     
Flag Coverage Δ
cpp 37.77% <ø> (ø)
csharp 87.11% <100.00%> (+<0.01%) ⬆️
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% <100.00%> (+0.26%) ⬆️

... and 9 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 0ce20c5 into master Dec 4, 2026
35 checks passed
@github-project-automation github-project-automation Bot moved this from Todo to Done in AngouriMath Dec 4, 2026
@Rafael-SOWNet
Rafael-SOWNet deleted the fix-2700-a-residual-at-a-large-modulus branch December 4, 2026 10:46
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.

A part that is exactly 0 is returned as a large number once the other part is large: cos(pi/2 + 1000 i) has a real part of -8.8E+332

1 participant