Repository navigation
Keep evaluation's digits from 1e-100000 to 1e100000 - #2674
Merged
Merged
Conversation
The default DecimalPrecisionContext had a hundred digits but exponents from -100 to 1000 only. An exact part below 10^-100 became 0 as a decimal and one past 10^1000 became +oo, so every digit built on it was wrong: sqrt(1/10^200 + 1/10^210) * 10^100 was 0, exp(3000) * exp(-3000) was NaN, and the exact roots Solve writes for an equation in 10^20 x evaluated to points with a residual of 1.36. The range is now -100000 to 100000, both ways, so that a number and its reciprocal are both held. With the range open, three precision limits that the flush had hidden showed, and are fixed with it: - the sine, cosine and arctangent ran in fixed point, whose guard digits are absolute, so below 10^-8 (10^-5 for the arctangent) they lost digits and at 10^-200 gave 0; they are now summed as series in floating point there; - the reduction of a sine's argument by 2 pi used pi to the working digits, so from 10^8 up it lost a digit per digit of the argument, and sin(10^500) had none right; it is now reduced with pi to as many more digits as the argument has, and an exact rational argument is written out to that many digits; - the Hermite reduction over two binomials of degree three or four was declined only because its check overflowed. Over the range, its answer to x^7 (a + b x^3)^(1/3)/(c + d x^3) is 23,030 characters, where the cube root's rules give 2,861. It is now declined for that pair, as the split by residues already declines it. Tests changed because their answers got better: the flushed gamma parts are asked for in a context ending at 1e-100, as the default did, with the default's values pinned beside them; catalannumber(2000.5), (1e600 + 1e600 i)^2 and three comparisons were +oo or stayed written, and are now their values, mpmath's. The rows still past the range moved past the new ends. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Codecov Report❌ Patch coverage is Additional details and impacted files
Flags with carried forward coverage won't be shown. Click here to find out more.
🚀 New features to boost your workflow:
|
…' reach (#2668) Widening the exponents let finite numbers past 1e1000 reach algorithms that had only ever seen +oo there, and some of them work through the argument's digits: - e^x for a huge x was taken by PeterO's exponential, which works its way to the overflow through a power of e as large as x, and inside ExpDifference in a context with no exponent range. sinh(exp(10000)) took 38 s to be +oo, and limitcheck went from 28 s to 44.5 s. Past ln 10 times the context's ends, e^x is now +oo or 0 from x's size alone, and e^v (e^d - 1) is e^(v+d) or -e^v once d is past them. - erf, erfc, erfi, Ei, Si, Ci, Shi, Chi, the gamma function, a quotient of factorials, polygamma off the real line, and the complex paths of exp, the trigonometric functions and arcsin/arccos did not finish in 20 s at 10^5000, or lost every digit to their residual scales. sin(10^5000 + i) had no imaginary part, and arcsin(10^5000) was 0. They take a part past 10^1000 as the infinity it was, so their answers there are master's. - e to a complex power now goes through Exp, as e to a real power does: the polar form multiplied the imaginary part by ln e's hundred digits, so cosh(10^5000 i) was -0.86 where it is cos(10^5000), -0.73. - zeta past a double's range counted its terms through doubles and threw on a negative precision, which zeta(-10^900) did on master too. It is left as written past a million. Measured: limitcheck 27.7/28.0 s on master and 28.3/28.1 s here, with 2 more cases compared and the same 0 wrong; sinh(exp(10000)) 38,604 ms -> 131 ms in-process. Suite 21993 passed. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…2668) A decimal argument is known to the working digits, so reducing it by 2 pi leaves precision - k of them for an argument near 10^k. Past 10^(precision - 16) the sine and cosine were mostly noise: cos(exp(300)) was 0.23 where it is -0.98. Widening the exponents let values past 10^1000 reach the same reduction, so cos(exp(10000)), which was NaN, became 0.31 where it is 0.92. SinAndCos is now NaN for a decimal past that bound. An exact rational is written out to the digits its reduction needs and goes through SinAndCosOfAnExactArgument at any size, so cos(10^150 + 1/3) and cos(10^150/3) are right. Si of such a decimal is +-pi/2, and Ei far up the imaginary axis is +-i pi, where the oscillating part is below the working digits. Suite 22012 passed; limitcheck 27.4/27.9 s on master, 27.7/28.1 s here. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This was referenced Dec 1, 2026
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 #2668.
What was wrong
DecimalPrecisionContextdefaulted toEContext(100, HalfUp, -100, 1000, false): a hundred digits, but exponents only from -100 to 1000. An exact part below10^-100became0as a decimal, and one past10^1000became+oo. Every digit built on such a part was wrong:sqrt(1/10^200 + 1/10^210) * 10^100was0, andexp(3000) * exp(-3000)wasNaN. On master the exact rootSolvewrites for((-2i) + 3y) + (-2 + 3y) abs(-3y) = 0withy = 10^20 xevaluated to(0.2627 + 0.4040i)/10^20, with residual 1.36. It now evaluates to(0.3931 + 0.2736i)/10^20, which is mpmath's root, with residual 0.The options, measured
sqrt(1/10^200+1/10^210)*10^100exp(3000)*exp(-3000)2^(-5000)*sqrt(2)*2^50000NaN0+oo+oo11.414…1.0e-200 * sqrt(2)and1/10^200 * sqrt(2)would also disagree. And it needs a per-evaluation decision of what counts as exact, inside[ThreadStatic]settings.NaNand0into+oo, because a saturated large part now meets a tiny part that is no longer 0. That is why the range is symmetric: a number and its reciprocal are both held.What came with it
With the range open, three limits that the flush had hidden showed. They are fixed here, because each would otherwise turn an old
0orNaNinto wrong digits:sin(1/10^200) * 10^200stayed0. Below10^-8(10^-5for the arctangent, whose guard is five digits) they are now summed as series in floating point. The loops compare as numbers: a zero argument's terms are zeros of ever smaller exponent, and a representation comparison ran 100,000 iterations on them. That hungintegral(ln(x) erf(x), x, 0, 1)in the first run of the suite. Its time now equals master's: 15.6 s against 15.9 s.2 pi. Pi was taken to the working digits, so from10^8up one digit was lost per digit of the argument.sin(10^500)was-0.2785, where it is0.3110. This was wrong on master too, and over the new range it would replacecos(10^5000) = NaNwith wrong digits. The argument is now reduced with pi taken to as many more digits as the argument has. Pi is cached per thread in 256-digit buckets, keyed by digit count, not byEContext, so the constant cache is not touched. An exact rational argument past10^8is written out to the same width first.sin(2^1000)was0.2915(master) and is-0.1592, mpmath's value.1e1000. Without the overflow,x^7 (a + b x^3)^(1/3)/(c + d x^3)integrates to a right antiderivative of 23,030 characters withc^50in it. The cube-root rules give 2,861. That pair is now declined explicitly, as the split by residues already declines it.Changed answers
Recorded in
BREAKING-CHANGES.md, measured on v2.10.0 and on this branch. Two rows of the #2664 entry changed:exp(-1000.5) > exp(-3002.5)andexp(-2000) = exp(-2000.5)are now decided (True,False) where they stayed written.Tests changed because the answer got better:
AFlushedPartOfTheGammaFunctionIsZeroTest: these parts no longer flush. The flush is now asked for in a context ending at1e-100, as the default did, and the default's values (mpmath) are pinned beside it.catalannumber(2000.5)is1.6614…E+1199(mpmath), not+oo.(1e600 + 1e600 i)^2is2e1200 i. The overflow rows moved to1e60000.Measurements
Measured against master
97f26c663, with the same harness binaries built from each tree.Issue2668_*tests fail on master: 14 of the 15 cases. The one that passes,sin(10^60), was swapped in forsin(10^20), whose master error of 1e-88 is inside the tolerance.Performance (
CommonFunctionsInterVersion, master then branch, one run each on the same machine under the chain lock). Allocation is unchanged: every gated benchmark is within 0.2% or 3 KB.SimplifyHardwent 134,718,976 → 134,711,008 B,SolveHard9,186,368 → 9,183,952 B, andSolveEasy419,515 → 418,695 B. Time moved between −2.6% and +3.2% per benchmark, with no direction:SolveHard+3.2%,ParseHard−2.6%,SimplifyEasy+0.8%,EvalTrig+0.2%,EvalTrigPrecise+0.5%.PerformanceGateagainstperformance-baseline.json: PASSED on all 19 gated benchmarks.Second commit: decide past-the-range values up front
A trial merge with master showed
limitcheckgoing from 28 s to 44.5 s. Almost all of that wassinh(exp(10000)). Before this branchexp(10000)was+oo, so the slow work was never reached. Now it is a finite 8.8e4342, and:ExpDifferencecalled it in a working context with no exponent range. Past ln 10 times the context's ends, e^x is now+ooor0, decided from x's size alone. InsideExpDifference,e^v (e^d - 1)ise^(v+d)or-e^voncedis past them.±10^5000,±exp(10000),10^5000 iand10^5000 + ifound these past 20 s, where master took 0.13 s:erf,erfc,erfi,Ei,Si,Ci,Shi,Chi,gamma, a quotient of factorials (catalannumber,binomial,risingfactorial), and polygamma off the real line. They now take a part past10^1000as the infinity it was, so their answers there are master's.exp,sin/cos/tan/cotandarcsin/arccosdropped digits past the reach to their residual scales:sin(10^5000 + i)had no imaginary part,arcsin(10^5000)was0, andtanh(10^5000 i)had a real part of -0.89. The same reach applies to them, so they are again master'sNaN/π/2 - ∞i.Exp, as e to a real power already did. The polar form multiplied the imaginary part by ln e's hundred digits, socosh(10^5000 i)was -0.86, where it is cos(10^5000) = -0.73.zetapast a double's range counted its terms through doubles and threw on a negative precision (zeta(-10^900)throws on master too). Past a million it is now left as written.Timings, in the same lock slot. Probes are per process, ~120 ms of which is startup:
limitcheck(twice each)sinh(exp(10000))tanh(exp(10000))sinh(10^5000)erf(10^5000)cos(sinh(10000))NaNsinh(exp(x))at +oosgn(erf(x)),log(erf(x), 2x)at ±oolimitchecknow compares 4872 cases where master compares 4870, with the same 0 wrong. Suite: 21,993 passed. F#: 135. All nine harnesses are unchanged from master, andPerformanceGatepasses. I re-ran the benchmark on master and on this commit back to back in one lock slot. Allocation is unchanged: every benchmark is within 0.06% or 1 KB (SolveHard9,185,040 → 9,186,408 B,SimplifyHard134,787,664 → 134,712,000 B). Time was −29% to +10% per benchmark in no direction (SolveHard−24%,ParseEasy+9.5%). The two runs of this commit against master, three hours apart, also disagreed by ±10%, so the time column is the machine, not the change.Third commit: no value for a periodic function of a decimal past its digits
A decimal argument is known only to the working digits, so reducing it by 2π leaves
precision - kof them for an argument near10^k. With the exponents widened,cos(exp(10000))(NaNbefore) came back0.31, where it is0.92. The same defect was already on master for any decimal past about10^84:cos(exp(300))was0.23where it is-0.98.SinAndCosis nowNaNfor a decimal past10^(precision - 16). An exact rational keeps the exact reduction, at any size. Every complex path goes throughSinAndCos:eto an imaginary power, the polar form of a power, and complex sin/cos/tan/cot.Siof such a decimal is±π/2, andEifar up the imaginary axis is±iπ, where the oscillating part is below the working digits. Changed answers are inBREAKING-CHANGES.md..Evaledcos(sinh(10000))cos(exp(10000))cos(10^150 + 1/3)cos(10^150/3)cos(exp(2000))cos(exp(300))cos(exp(250))sin(exp(300))cos(sinh(300))cos(sqrt(2)*10^200)cos(pi*10^200)tan(exp(500))sin(exp(190))(just inside the bound)Si(exp(300))Ei(i exp(300))Suite: 22,012 passed. F#: 135 passed.
limitcheck: 27.4 s and 27.9 s on master, 27.7 s and 28.1 s here.🤖 Generated with Claude Code