Skip to content

Give cos, sin and tan of an imaginary part past the exponents a value - #2699

Merged
Rafael-SOWNet merged 1 commit into
masterfrom
fix-2698-trig-of-a-large-imaginary-argument
Dec 4, 2026
Merged

Rafael-SOWNet merged 1 commit into
masterfrom
fix-2698-trig-of-a-large-imaginary-argument

Conversation

@Rafael-SOWNet

Copy link
Copy Markdown
Member

Fixes #2698

Cause

From an imaginary part near 230259, cosh and sinh of it are past the context's exponents (about 1e100001) and infinite. A part of cos(x + i y) or sin(x + i y) is the cosine or the sine of x times one of them, and where that factor is 0 the product was 0 * +oo, NaN, which Complex.Create makes the whole number: cos(10^6 i) was NaN where cosh(10^6) is +oo.

The issue guessed that tan is sin/cos, with both overflowing. It is not: Tan uses (sin 2x / cosh 2y + i tanh 2y) / (1 + cos 2x / cosh 2y), and InternalAMExtensions.Tanh took tanh(2y) as (e^x - e^-x)/(e^x + e^-x), which past the exponents is +oo / +oo, NaN. A short cut in Tanh alone made every tan row right, before anything in Sin/Cos changed.

sec and csc were 1 / cos and 1 / sin. A quotient by an infinity off the real line is NaN in this library, so sec(1 + 10^6 i) stayed NaN even where cos was +oo - +ooi.

Change

  • Sin and Cos: where cosh(im) is infinite, each part goes through TimesAHyperbolicPastTheExponents:
    • It is 0 where its factor is 0, or a residual of x against the same scale the finite case uses. So cos(pi/2 + 10^6 i) is -ooi. On master it was -oo - +ooi: the residual of cos(pi/2) set the real part's sign, which gave the wrong direction.
    • Otherwise it is the infinity of its sign, unless |factor| e^|y| / 2 is still within the exponents. Then it is that number, worked out as e^(|y| + ln|factor| - ln 2). cos(10^-40 + 230300 i) has imaginary part -5.2257944491…E+99977, right to 1.3e-101; master said -oo.
  • A direction off the axes is written as exp(10^6 + i) already writes it, with each part the infinity of its sign: cos(1 + 10^6 i) = +oo - +ooi.
  • Tanh: returns its sign past (digits + 3) ln 10, where 1 - |tanh| is below the last digit.
  • Secant/Cosecant: return 0 where cos/sin has an infinite part and no NaN part. This matches exp(-10^6), which is 0.
  • Unchanged (the Keep evaluation's digits from 1e-100000 to 1e100000 #2674 policy): an inexact real part past the digits its reduction needs leaves the factor unknown. cos(10^200 sqrt(2) + 10^6 i) is still NaN.

Measurements (master 82eb869, branch 2fac2fb)

Sweep against mpmath at 450 digits. 2184 values of cos sin tan cot sec csc cosh sinh tanh coth sech csch exp, of ±10^k i and a ± 10^k i for a in 1, -1, 1/2 and pi/4 and k in 1..12, 50, 100 and 300, plus 18 ordinary arguments.

right (rel 1e-90) overflow-correct NaN WRONG pole
master 1648 160 368 4 4
branch 1948 200 28 4 4

Allocation. GC.GetAllocatedBytesForCurrentThread over 3000 ops after a warm-up; two runs each, alternating.

case master B/op branch B/op Δ
ln(1 + x^2)*arctan(x) at 0.37000000000001234 (EvalTranscendentalFresh) 34680 34680 0.00%
sin(x) / cos(x) / tan(x) at 0.37 21992 / 21992 / 24896 same 0.00%
sin / cos / tan / cot / sec / csc at 1 + 2i 51610 / 51840 / 60464 / 80104 / 74528 / 73584 51614 / 51840 / 60464 / 80104 / 74528 / 73584 ≤ +0.01%
tan(x) at 1 + 10i (the tanh path) 91779 91875 +0.10%
cos(x) at 1/3 + 300i 61520 61520 0.00%

Suites and harnesses

  • C# suite: 22253 passed, 13 skipped, 0 failed (8 m 31 s).
  • F# suite: 135 passed.
  • limitcheck and symlimitcheck: reports byte-identical between master and the branch, on both alternating runs. limitcheck took 28.6 s / 28.3 s on master and 28.8 s / 28.8 s on the branch; symlimitcheck took 24.5 s / 24.6 s on master and 24.8 s / 24.7 s on the branch.

The regression tests are Issue2698_* in NumericsRegressionTest. The BREAKING-CHANGES.md entry sits before "A number past the working digits is not taken for an exact one".

🤖 Generated with Claude Code

From an imaginary part near 230259, cosh and sinh of it are past the
context's exponents and infinite, and a part of cos(x + i y) or
sin(x + i y) whose factor, the cosine or the sine of x, is 0 was 0 times
that infinity, NaN, which made the whole number NaN: cos(10^6 i) was NaN
where cosh(10^6) is +oo, and sin(10^6 i) where it is +oo i. Such a part
is now 0 where its factor is 0 or a residual of x as on the real line,
the infinity of its sign otherwise, and the number it is where a small
factor brings it back within the exponents. A direction off the axes is
written as exp(10^6 + i) already writes it, each part the infinity of
its sign; cos(pi/2 + 10^6 i) is -oo i, where its residual real part had
made it -oo - oo i.

tan(x + i y) is not sin/cos: it takes tanh(2 y) through e^(2 y), which
past the exponents was +oo / +oo, NaN. tanh is its sign from
(digits + 3) ln 10, past which 1 - |tanh| is below the last digit, so
tan(10^6 i) is i and tan(1/2 - 10^300 i) is -i. sec and csc of an
infinity off the real line were NaN, a quotient by it, and are 0.

A decimal real part past the digits its reduction by 2 pi needs leaves
the factor unknown, and cos(10^200 sqrt(2) + 10^6 i) is NaN as before.

Of 2184 values of cos, sin, tan, cot, sec, csc, cosh, sinh, tanh, coth,
sech, csch and exp of ±10^k i and a ± 10^k i (a in 1, -1, 1/2, pi/4;
k = 1..12, 50, 100, 300) and eighteen ordinary arguments, checked against
mpmath at 450 digits, master had 368 NaN; this has 300 of them right to
a relative 1e-90 and 40 the infinity of each part's sign, and no row
right on master changed.

Fixes #2698

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@codecov

codecov Bot commented Dec 4, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 96.66667% with 1 line in your changes missing coverage. Please review.
✅ Project coverage is 86.63%. Comparing base (82eb869) to head (2fac2fb).

Files with missing lines Patch % Lines
...uriMath/Core/Entity/Continuous/Number/Operators.cs 96.42% 0 Missing and 1 partial ⚠️
Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #2699      +/-   ##
==========================================
+ Coverage   86.61%   86.63%   +0.01%     
==========================================
  Files         346      346              
  Lines       76754    76782      +28     
  Branches    22554    22566      +12     
==========================================
+ Hits        66483    66521      +38     
+ Misses       5546     5535      -11     
- Partials     4725     4726       +1     
Flag Coverage Δ
cpp 37.77% <ø> (ø)
csharp 87.12% <96.66%> (+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 Δ
...rces/AngouriMath/Functions/InternalAMExtensions.cs 85.85% <100.00%> (+0.04%) ⬆️
...uriMath/Core/Entity/Continuous/Number/Operators.cs 90.37% <96.42%> (+0.22%) ⬆️

... and 4 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 28bec0a 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-2698-trig-of-a-large-imaginary-argument branch December 4, 2026 03:30
Rafael-SOWNet added a commit that referenced this pull request Dec 4, 2026
…at any size (#2703)

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>
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.

cos, sin and tan of a large imaginary argument are NaN where cosh gives +oo and tan is i

1 participant