Repository navigation
Keep the small parts of a logarithm near 1, a root and Ci (#2677) - #2681
Open
Rafael-SOWNet wants to merge 3 commits into
Open
Rafael-SOWNet wants to merge 3 commits into
Rafael-SOWNet wants to merge 3 commits into
Conversation
A logarithm took ln(1 + t) for the residual of a cancellation against 1 below half the working digits, so ln(1 + 10^(-60)) was 0, and with it arcoth, arcsch, artanh and arsech past 10^50, which are logarithms of 1 + 1/x. A logarithm whose argument's modulus is within 10^(-25) of 1, or is a 0 or 1 an inexact expression evaluated to, now evaluates its arguments again with the digits it cancels; a residual 1 stays 0. A root of a number with a decimal imaginary part other than 0 is no longer searched for among the reals, which read a phase of 10^(-60) as 0: sqrt(1 + 10^(-60) i) was 1. Ci and Si past the series measured the constant pi/2 branch terms they add and take away as terms of their own size, so Ci(10^60 + i) lost its imaginary part; a constant is a residual only against its last digits. AComplexFunctionKeepsASmallPartTest pinned the real part of ln(1 + 10^(-60) i) as 0; it is 5e-121, and the test now says so. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Evaluating again every logarithm of a 1 an inexact expression evaluated to cost limitcheck 15% of its time, measured by ablation: 32.0 s with the 1s, 29.2 s with the 0s alone, 27.8 s with neither. A 0 is now evaluated again only where the argument is a sum, where a cancellation can leave it. ln(e^(10^(-60))), whose argument the downcasting rounds onto 1, stays 0, and its test row and table row go. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## master #2681 +/- ##
==========================================
+ Coverage 86.64% 86.65% +0.01%
==========================================
Files 346 346
Lines 76166 76258 +92
Branches 22329 22357 +28
==========================================
+ Hits 65991 66083 +92
+ Misses 5503 5502 -1
- Partials 4672 4673 +1
Flags with carried forward coverage won't be shown. Click here to find out more.
🚀 New features to boost your workflow:
|
…2677) The solver writes a trigonometric root as ln(a + b i) / i, with a and b decimals to the working digits and |a + b i| = 1 to within their rounding, about 10^(-101). Evaluated again with more digits, a written decimal is the same decimal, so that distance read as placed, and the logarithm returned it: the real roots of cos(3x) = sin(2x) and sin(2x) = sin(3x) came back with imaginary parts near 10^(-101), and trigcheck dropped them under scaling. Where either argument has a decimal in it, a distance is known to the working digits alone, and a 0 a sum left is not evaluated again. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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 #2677
What was wrong
On master (e3b3a34, after #2676) a part of a value far smaller than the rest was dropped:
arcoth(10^60)andarcsch(10^60)were 0 where they are1E-60,arsech(10^50 * i)lost its real part1E-50,(1 + 10^(-50) * i)^(1/2)was 1 where it is1 + 5E-51i, andCi(10^60 + i)lost its imaginary part-6.548E-61. In the same class:ln(1 + 10^(-60))was 0,arsinh(-10^60 * i)andarcsch(10^(-60) * i)were-oo, and every root of1 + 10^(-k) ihad no imaginary part.Causes, each confirmed before it was fixed
arcoth(x) = 1/2 ln((x + 1)/(x - 1))(Convenience/MathS.cs:2675and its neighbours), so far out each isln(1 + t)for a tinyt.Number.Lnmakes a real logarithm at the scale of its argument (Core/Entity/Continuous/Number/Operators.cs:941on master), andReal.CreateAtScalerounds a value below the tolerance times the scale onto 0 (Entity.Continuous.Real.Definition.cs:147). At a hundred digits the tolerance is1E-50, soln(1 + 2E-60) = 2E-60was 0. The exact argument ofarcothand the inexact one ofarcschfail the same way, and so doesln(1 + 10^(-60))written out.10^(-60)as 0.1 + 10^(-60) * ievaluates with a decimal imaginary part, so Keep every part of a complex value past 10^50 (#2675) #2676's guard for an exact imaginary part does not apply.FindGoodRoot(Operators.cs:772) went on toGetAllRoots, whose phaseLn(value / |value|) / i(Operators.cs:370) came out 0, which I measured as the roots returned and did not trace further, andIsZeroAtTheWorkingPrecision(Pow(rational, power) - base)(Operators.cs:460) accepted1as the root, because1E-60 iis under1E-50.Ci(z) = Chi(iz) - i pi/2,Chiadds+-pi/2, andEi(z)andEi(-z)add+-pieach (TrigonometricIntegrals.cs:194, 212;ExponentialIntegrals.cs:209). They cancel to-6.5E-61 i, and each was added to the imaginary scale at full size, so the scale was about2 piand the value was below the tolerance times it.The fix
Logf's numerical evaluation (Functions/Evaluation/Evaluation.Continuous/Evaluation.Continuous.Arithmetics.Classes.cs:600,NearOneWithMoreDigitsat :673) evaluates the base and the antilogarithm again, with more digits, where the modulus of either is within10^(-precision/4)of 1. It takesprecision + kand2kdigits for a distance10^(-k), so the value is above half of them and keeps all of its own. A rational's distance is exact. A decimal distance below the working digits is not known, so the digits are doubled, twice at most. A 0 that a sum other than exact arithmetic evaluated to is treated the same way:arsinh(-10^60 i)isln(-10^60 i + sqrt(1 - 10^120)), whose argument cancels to-5E-61 i. The wider contexts are cached, since the constants are cached by context instance. A logarithm of a 1 that is a residual, such asln(sqrt(2)^2/2)orln(sin(3)^2 + cos(3)^2), stays a residual at more digits and is still 0. Where either argument has a decimal written in it, such as a root the solver wrote asln(a + b i) / i, evaluating it again does not add digits: its distance from 1 is known to the working digits only, and below them it is the decimal's own rounding. The first version of this PR missed that. It read|a + b i| - 1 ~ 1e-101as a value, and the real roots ofcos(3x) = sin(2x)andsin(2x) = sin(3x)came back with imaginary parts near1e-101, which trigcheck caught under scaling. This is the brief's "area function evaluated with log1p-style care", made at the logarithm, so it covers every spelling ofln(1 + t)and not only the six area functions. Keeping six new nodes would have meant teaching differentiation, simplification, integration, limits, the solver, the printers and the compilers about each one.FindGoodRootreturns no real root for a base with a decimal imaginary part other than 0 (Operators.cs:429), as Keep every part of a complex value past 10^50 (#2675) #2676 already did for an exact one. A number keeps such a part only when its own creation judged it no residual. The base goes to the polar form, which keeps the phase.GetAllRootsis unchanged: Keep every part of a complex value past 10^50 (#2675) #2676 measured that changing it mixed a complex principal root with a real sibling and gave-1.pi * 10^(-guard)(ExponentialIntegrals.cs:218,TrigonometricIntegrals.cs:198, 219, 224). A constant cancels nothing, which is the same accounting as the series'ScaleByPowerOfTen(beyond).Each fix links the v3 decision (#1019), since each answers a residual of the downcasting.
Sweep
(mpmath, dps 1200): every area function,arcsec,arccscandarccotatx,x iand-x iforxin 10^10, 10^20, 10^60, 10^200 and 10^-60;sqrt,(1 + t)^(1/3),(1 + t)^(2/3),(1 + t)^(-1/2)andln(1 + t)fort = +-10^(-k)and+-10^(-k) i, k in 10, 20, 60 and 200; andCi,SiandEiatx + i,x - i,1 + x iand-1 + x i. That is 275 rows. Each part is held to a relative 1e-10, and a part that is 0 to exactly 0.The 43 rows that changed:
arsinh(-10^60 * i)arsinh(10^(-60))arcosh(10^(-60) * i)arcosh(-10^(-60) * i)artanh(10^60)artanh(10^200)artanh(10^(-60))arcoth(10^60)arcoth(10^200)arcoth(10^(-60))arsech(10^60 * i)arsech(-10^60 * i)arsech(10^200 * i)arsech(-10^200 * i)arcsch(10^60)arcsch(10^200)arcsch(10^(-60) * i)ln(1 + 10^(-60))ln(1 + -10^(-60))sqrt(1 + 10^(-60) * i)(1 + 10^(-60) * i)^(1/3)(1 + 10^(-60) * i)^(2/3)(1 + 10^(-60) * i)^(-1/2)ln(1 + 10^(-60) * i)sqrt(1 + -10^(-60) * i)(1 + -10^(-60) * i)^(1/3)(1 + -10^(-60) * i)^(2/3)(1 + -10^(-60) * i)^(-1/2)ln(1 + -10^(-60) * i)ln(1 + 10^(-200))ln(1 + -10^(-200))sqrt(1 + 10^(-200) * i)(1 + 10^(-200) * i)^(1/3)(1 + 10^(-200) * i)^(2/3)(1 + 10^(-200) * i)^(-1/2)ln(1 + 10^(-200) * i)sqrt(1 + -10^(-200) * i)(1 + -10^(-200) * i)^(1/3)(1 + -10^(-200) * i)^(2/3)(1 + -10^(-200) * i)^(-1/2)ln(1 + -10^(-200) * i)Ci(10^60 + i)Ci(10^60 - i)The 31 rows still wrong, unchanged from master:
Ci,SiandEioverflow past10^100000(e^(10^10)and up). They give+-ooorNaN, as on master.Ei(+-1 + 10^200 * i)returnsi piwith no real part (2.6E-200). This is An exact expression whose parts are below 10^-100 evaluates to wrong digits: sqrt(1/10^200 + 1/10^210) * 10^100 is 0 #2668's deliberate cut: a decimal imaginary part past10^(precision - 16)has no cosine.10^200 * ievaluates to such a decimal, because a Complex holds its parts as decimals.arsinh(-10^200 * i)is still-oo. Its argument cancels terms of10^200down to5E-201, which needs more than the two doublings allow.All 275 rows
arsinh(10^10)arsinh(10^10 * i)arsinh(-10^10 * i)arsinh(10^20)arsinh(10^20 * i)arsinh(-10^20 * i)arsinh(10^60)arsinh(10^60 * i)arsinh(-10^60 * i)arsinh(10^200)arsinh(10^200 * i)arsinh(-10^200 * i)arsinh(10^(-60))arsinh(10^(-60) * i)arsinh(-10^(-60) * i)arcosh(10^10)arcosh(10^10 * i)arcosh(-10^10 * i)arcosh(10^20)arcosh(10^20 * i)arcosh(-10^20 * i)arcosh(10^60)arcosh(10^60 * i)arcosh(-10^60 * i)arcosh(10^200)arcosh(10^200 * i)arcosh(-10^200 * i)arcosh(10^(-60))arcosh(10^(-60) * i)arcosh(-10^(-60) * i)artanh(10^10)artanh(10^10 * i)artanh(-10^10 * i)artanh(10^20)artanh(10^20 * i)artanh(-10^20 * i)artanh(10^60)artanh(10^60 * i)artanh(-10^60 * i)artanh(10^200)artanh(10^200 * i)artanh(-10^200 * i)artanh(10^(-60))artanh(10^(-60) * i)artanh(-10^(-60) * i)arcoth(10^10)arcoth(10^10 * i)arcoth(-10^10 * i)arcoth(10^20)arcoth(10^20 * i)arcoth(-10^20 * i)arcoth(10^60)arcoth(10^60 * i)arcoth(-10^60 * i)arcoth(10^200)arcoth(10^200 * i)arcoth(-10^200 * i)arcoth(10^(-60))arcoth(10^(-60) * i)arcoth(-10^(-60) * i)arsech(10^10)arsech(10^10 * i)arsech(-10^10 * i)arsech(10^20)arsech(10^20 * i)arsech(-10^20 * i)arsech(10^60)arsech(10^60 * i)arsech(-10^60 * i)arsech(10^200)arsech(10^200 * i)arsech(-10^200 * i)arsech(10^(-60))arsech(10^(-60) * i)arsech(-10^(-60) * i)arcsch(10^10)arcsch(10^10 * i)arcsch(-10^10 * i)arcsch(10^20)arcsch(10^20 * i)arcsch(-10^20 * i)arcsch(10^60)arcsch(10^60 * i)arcsch(-10^60 * i)arcsch(10^200)arcsch(10^200 * i)arcsch(-10^200 * i)arcsch(10^(-60))arcsch(10^(-60) * i)arcsch(-10^(-60) * i)arcsec(10^10)arcsec(10^10 * i)arcsec(-10^10 * i)arcsec(10^20)arcsec(10^20 * i)arcsec(-10^20 * i)arcsec(10^60)arcsec(10^60 * i)arcsec(-10^60 * i)arcsec(10^200)arcsec(10^200 * i)arcsec(-10^200 * i)arcsec(10^(-60))arcsec(10^(-60) * i)arcsec(-10^(-60) * i)arccsc(10^10)arccsc(10^10 * i)arccsc(-10^10 * i)arccsc(10^20)arccsc(10^20 * i)arccsc(-10^20 * i)arccsc(10^60)arccsc(10^60 * i)arccsc(-10^60 * i)arccsc(10^200)arccsc(10^200 * i)arccsc(-10^200 * i)arccsc(10^(-60))arccsc(10^(-60) * i)arccsc(-10^(-60) * i)arccot(10^10)arccot(10^10 * i)arccot(-10^10 * i)arccot(10^20)arccot(10^20 * i)arccot(-10^20 * i)arccot(10^60)arccot(10^60 * i)arccot(-10^60 * i)arccot(10^200)arccot(10^200 * i)arccot(-10^200 * i)arccot(10^(-60))arccot(10^(-60) * i)arccot(-10^(-60) * i)sqrt(1 + 10^(-10))(1 + 10^(-10))^(1/3)(1 + 10^(-10))^(2/3)(1 + 10^(-10))^(-1/2)ln(1 + 10^(-10))sqrt(1 + -10^(-10))(1 + -10^(-10))^(1/3)(1 + -10^(-10))^(2/3)(1 + -10^(-10))^(-1/2)ln(1 + -10^(-10))sqrt(1 + 10^(-10) * i)(1 + 10^(-10) * i)^(1/3)(1 + 10^(-10) * i)^(2/3)(1 + 10^(-10) * i)^(-1/2)ln(1 + 10^(-10) * i)sqrt(1 + -10^(-10) * i)(1 + -10^(-10) * i)^(1/3)(1 + -10^(-10) * i)^(2/3)(1 + -10^(-10) * i)^(-1/2)ln(1 + -10^(-10) * i)sqrt(1 + 10^(-20))(1 + 10^(-20))^(1/3)(1 + 10^(-20))^(2/3)(1 + 10^(-20))^(-1/2)ln(1 + 10^(-20))sqrt(1 + -10^(-20))(1 + -10^(-20))^(1/3)(1 + -10^(-20))^(2/3)(1 + -10^(-20))^(-1/2)ln(1 + -10^(-20))sqrt(1 + 10^(-20) * i)(1 + 10^(-20) * i)^(1/3)(1 + 10^(-20) * i)^(2/3)(1 + 10^(-20) * i)^(-1/2)ln(1 + 10^(-20) * i)sqrt(1 + -10^(-20) * i)(1 + -10^(-20) * i)^(1/3)(1 + -10^(-20) * i)^(2/3)(1 + -10^(-20) * i)^(-1/2)ln(1 + -10^(-20) * i)sqrt(1 + 10^(-60))(1 + 10^(-60))^(1/3)(1 + 10^(-60))^(2/3)(1 + 10^(-60))^(-1/2)ln(1 + 10^(-60))sqrt(1 + -10^(-60))(1 + -10^(-60))^(1/3)(1 + -10^(-60))^(2/3)(1 + -10^(-60))^(-1/2)ln(1 + -10^(-60))sqrt(1 + 10^(-60) * i)(1 + 10^(-60) * i)^(1/3)(1 + 10^(-60) * i)^(2/3)(1 + 10^(-60) * i)^(-1/2)ln(1 + 10^(-60) * i)sqrt(1 + -10^(-60) * i)(1 + -10^(-60) * i)^(1/3)(1 + -10^(-60) * i)^(2/3)(1 + -10^(-60) * i)^(-1/2)ln(1 + -10^(-60) * i)sqrt(1 + 10^(-200))(1 + 10^(-200))^(1/3)(1 + 10^(-200))^(2/3)(1 + 10^(-200))^(-1/2)ln(1 + 10^(-200))sqrt(1 + -10^(-200))(1 + -10^(-200))^(1/3)(1 + -10^(-200))^(2/3)(1 + -10^(-200))^(-1/2)ln(1 + -10^(-200))sqrt(1 + 10^(-200) * i)(1 + 10^(-200) * i)^(1/3)(1 + 10^(-200) * i)^(2/3)(1 + 10^(-200) * i)^(-1/2)ln(1 + 10^(-200) * i)sqrt(1 + -10^(-200) * i)(1 + -10^(-200) * i)^(1/3)(1 + -10^(-200) * i)^(2/3)(1 + -10^(-200) * i)^(-1/2)ln(1 + -10^(-200) * i)Ci(10^10 + i)Ci(1 + 10^10 * i)Ci(10^10 - i)Ci(-1 + 10^10 * i)Ci(10^20 + i)Ci(1 + 10^20 * i)Ci(10^20 - i)Ci(-1 + 10^20 * i)Ci(10^60 + i)Ci(1 + 10^60 * i)Ci(10^60 - i)Ci(-1 + 10^60 * i)Ci(10^200 + i)Ci(1 + 10^200 * i)Ci(10^200 - i)Ci(-1 + 10^200 * i)Ci(10^(-60) + i)Ci(1 + 10^(-60) * i)Ci(10^(-60) - i)Ci(-1 + 10^(-60) * i)Si(10^10 + i)Si(1 + 10^10 * i)Si(10^10 - i)Si(-1 + 10^10 * i)Si(10^20 + i)Si(1 + 10^20 * i)Si(10^20 - i)Si(-1 + 10^20 * i)Si(10^60 + i)Si(1 + 10^60 * i)Si(10^60 - i)Si(-1 + 10^60 * i)Si(10^200 + i)Si(1 + 10^200 * i)Si(10^200 - i)Si(-1 + 10^200 * i)Si(10^(-60) + i)Si(1 + 10^(-60) * i)Si(10^(-60) - i)Si(-1 + 10^(-60) * i)Ei(10^10 + i)Ei(1 + 10^10 * i)Ei(10^10 - i)Ei(-1 + 10^10 * i)Ei(10^20 + i)Ei(1 + 10^20 * i)Ei(10^20 - i)Ei(-1 + 10^20 * i)Ei(10^60 + i)Ei(1 + 10^60 * i)Ei(10^60 - i)Ei(-1 + 10^60 * i)Ei(10^200 + i)Ei(1 + 10^200 * i)Ei(10^200 - i)Ei(-1 + 10^200 * i)Ei(10^(-60) + i)Ei(1 + 10^(-60) * i)Ei(10^(-60) - i)Ei(-1 + 10^(-60) * i)Measurements
dotnet test Sources/Tests/UnitTests -f net10.0: 22068 passed, 0 failed, 13 skipped.FSharpWrapperUnitTests: 135 passed. Then the two new solver rows,Issue2677_TheLogarithmOfAWrittenDecimalOnTheUnitCircleIsImaginary, were run with the other arcoth, arcsch, arsech, a square root and Ci drop a part much smaller than their input #2677 tests: 32 passed. Before the last commit their roots' logarithms had real parts near1e-101.trigcheck under scaling, at 10^-12, 10^12, 10^30 and 10^-30, for
eqs1121,eqsexp1121andeqsinv1121: 0 differ in each of the 12 runs.The 275-row sweep has the same verdicts after the last commit: the 43 wrong → right rows hold.
One existing test changed.
AComplexFunctionKeepsASmallPartTestpinned the real part ofln(1 + 10^(-60) * i)as0. It is5E-121,ln|1 + 10^(-60) i|. The answer got better, and the test now says so.limitcheck, 600 expressions, same verdicts on both builds (compared 4872, 0 wrong). Timings alternate between the builds on one machine under the chain lock, user CPU in seconds:
Against the master runs alternated with it, the last commit costs about +1.4% (29.2 against 28.8). The first version also evaluated again every 1 that an inexact expression evaluated to, which was +17%. Ablation put that on the 1s: 32.0 s with them, 29.2 s with the 0s alone, 27.8 s with neither. The 1s were dropped. The cost of that is that
ln(e^(10^(-60)))stays 0, because the downcasting roundse^(10^(-60))onto the integer 1 before the logarithm sees it.The sweep's 275 rows took 0.84 s on master and 1.41 s on the branch. The difference is the rows that are now evaluated again, at up to 820 digits.
Changed answers
All are in
BREAKING-CHANGES.md. Two of them are not dropped parts:(-8 + 10^(-90) * i)^(1/3)is now the principal root1 + 1.732i, where it was the real root-2. The base is not real, so the "odd roots of negative reals are real" convention does not reach it.log(1 + 10^(-60), 2)gains digits in its last places:…680.36066…against master's…680.36524…. Its true value isln 2 / 10^(-60).🤖 Generated with Claude Code