Repository navigation
Give cos, sin and tan of an imaginary part past the exponents a value - #2699
Merged
Merged
Conversation
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 Report❌ Patch coverage is
Additional details and impacted files@@ 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
Flags with carried forward coverage won't be shown. Click here to find out more.
🚀 New features to boost your workflow:
|
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>
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 #2698
Cause
From an imaginary part near 230259,
coshandsinhof it are past the context's exponents (about1e100001) and infinite. A part ofcos(x + i y)orsin(x + i y)is the cosine or the sine ofxtimes one of them, and where that factor is 0 the product was0 * +oo, NaN, whichComplex.Createmakes the whole number:cos(10^6 i)was NaN wherecosh(10^6)is+oo.The issue guessed that
tanissin/cos, with both overflowing. It is not:Tanuses(sin 2x / cosh 2y + i tanh 2y) / (1 + cos 2x / cosh 2y), andInternalAMExtensions.Tanhtooktanh(2y)as(e^x - e^-x)/(e^x + e^-x), which past the exponents is+oo / +oo, NaN. A short cut inTanhalone made everytanrow right, before anything inSin/Coschanged.secandcscwere1 / cosand1 / sin. A quotient by an infinity off the real line is NaN in this library, sosec(1 + 10^6 i)stayed NaN even wherecoswas+oo - +ooi.Change
SinandCos: wherecosh(im)is infinite, each part goes throughTimesAHyperbolicPastTheExponents:xagainst the same scale the finite case uses. Socos(pi/2 + 10^6 i)is-ooi. On master it was-oo - +ooi: the residual ofcos(pi/2)set the real part's sign, which gave the wrong direction.|factor| e^|y| / 2is still within the exponents. Then it is that number, worked out ase^(|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.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, where1 - |tanh|is below the last digit.Secant/Cosecant: return 0 wherecos/sinhas an infinite part and no NaN part. This matchesexp(-10^6), which is 0.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 ianda ± 10^k iforain 1, -1, 1/2 and pi/4 andkin 1..12, 50, 100 and 300, plus 18 ordinary arguments.expofpi/4 ± 10^100 iandpi/4 ± 10^300 i. There the evaluated argument's imaginary part is a decimal, so the Keep evaluation's digits from 1e-100000 to 1e100000 #2674/A complex number's exact parts become decimals past 10^50, so exp(10^84 i) and cos(10^84 + i) are NaN #2685 policy applies. They were NaN on master too.sec/csc(1/2 - 30 i)are right to 88 digits, not 90 (the complex reciprocal).tanh/coth(1e-60 + i)drop a real part near 1e-60.Allocation.
GC.GetAllocatedBytesForCurrentThreadover 3000 ops after a warm-up; two runs each, alternating.ln(1 + x^2)*arctan(x)at 0.37000000000001234 (EvalTranscendentalFresh)sin(x)/cos(x)/tan(x)at 0.37sin/cos/tan/cot/sec/cscat 1 + 2itan(x)at 1 + 10i (thetanhpath)cos(x)at 1/3 + 300iSuites and harnesses
The regression tests are
Issue2698_*inNumericsRegressionTest. TheBREAKING-CHANGES.mdentry sits before "A number past the working digits is not taken for an exact one".🤖 Generated with Claude Code