Skip to content

Keep evaluation's digits from 1e-100000 to 1e100000 - #2674

Merged
Rafael-SOWNet merged 3 commits into
masterfrom
exact-evaluation-below-the-floor-2668
Dec 1, 2026
Merged

Rafael-SOWNet merged 3 commits into
masterfrom
exact-evaluation-below-the-floor-2668

Conversation

@Rafael-SOWNet

@Rafael-SOWNet Rafael-SOWNet commented Nov 30, 2026 •

Copy link
Copy Markdown
Member

Fixes #2668.

What was wrong

DecimalPrecisionContext defaulted to EContext(100, HalfUp, -100, 1000, false): a hundred digits, but exponents only from -100 to 1000. An exact part below 10^-100 became 0 as a decimal, and one past 10^1000 became +oo. Every digit built on such a part was wrong: sqrt(1/10^200 + 1/10^210) * 10^100 was 0, and exp(3000) * exp(-3000) was NaN. On master the exact root Solve writes for ((-2i) + 3y) + (-2 + 3y) abs(-3y) = 0 with y = 10^20 x evaluated 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^100 exp(3000)*exp(-3000) 2^(-5000)*sqrt(2)*2^5000 suite
master, -100..1000 0 NaN 0 —
(b) floor only, -100000..1000 right +oo +oo —
(b) both ends, -100000..100000 right 1 1.414… 10 tests changed: 9 values became right, and 1 antiderivative grew to 23,030 characters (item 3 below)
  • (a) dropping the floor for exact inputs only is (b) applied to some evaluations. Every one of the suite's changes under (b) is on an exact input, so (a) would change the same answers. A decimal literal is not less exact in magnitude than a rational, so 1.0e-200 * sqrt(2) and 1/10^200 * sqrt(2) would also disagree. And it needs a per-evaluation decision of what counts as exact, inside [ThreadStatic] settings.
  • (c) declining a result built on a flushed part gives no digits where (b) gives the right ones.
  • Moving only the floor turns NaN and 0 into +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 0 or NaN into wrong digits:

  1. The sine, cosine and arctangent of a tiny argument. They run in fixed point, whose guard digits are absolute, so sin(1/10^200) * 10^200 stayed 0. Below 10^-8 (10^-5 for 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 hung integral(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. The reduction of a large argument by 2 pi. Pi was taken to the working digits, so from 10^8 up one digit was lost per digit of the argument. sin(10^500) was -0.2785, where it is 0.3110. This was wrong on master too, and over the new range it would replace cos(10^5000) = NaN with 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 by EContext, so the constant cache is not touched. An exact rational argument past 10^8 is written out to the same width first. sin(2^1000) was 0.2915 (master) and is -0.1592, mpmath's value.
  3. The Hermite reduction over two binomials of degree three or four. It was declined only because its own numeric check overflowed past 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 with c^50 in 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) and exp(-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 at 1e-100, as the default did, and the default's values (mpmath) are pinned beside it.
  • catalannumber(2000.5) is 1.6614…E+1199 (mpmath), not +oo.
  • (1e600 + 1e600 i)^2 is 2e1200 i. The overflow rows moved to 1e60000.
  • Three comparisons moved past the new ends, and three that are now decided moved to the decided list.

Measurements

Measured against master 97f26c663, with the same harness binaries built from each tree.

  • C# suite: 21,981 passed, 0 failed, 13 skipped. The library was built fresh and the build had no errors. F# suite: 135 passed.
  • The new Issue2668_* tests fail on master: 14 of the 15 cases. The one that passes, sin(10^60), was swapped in for sin(10^20), whose master error of 1e-88 is inside the tolerance.
harness master branch
CasBench 120/121 solved, 1 unsolved, 0 wrong, 0 error, 0 timeout identical
SimpSweep 10463/10463 agree, 0 disagree, 0 timed out identical
BoundCheck 1652 shapes, 63 rewritten, 0 disagreements, 0 did not finish identical, report byte-equal
PropCheck 1355 checks, 0 failures identical
RootCheck 596/596 clean identical
CanonCheck matches baseline (2040) identical
RuleCheck 30 sets, 0 never settle, 0 value changes identical
Confluence 3 of 11 overlapping pairs conflict, matches baseline identical
CrashCheck 2618 cases, 0 crashed, 0 did not finish, 2459 returned, 159 declined identical

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. SimplifyHard went 134,718,976 → 134,711,008 B, SolveHard 9,186,368 → 9,183,952 B, and SolveEasy 419,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%. PerformanceGate against performance-baseline.json: PASSED on all 19 gated benchmarks.

Second commit: decide past-the-range values up front

A trial merge with master showed limitcheck going from 28 s to 44.5 s. Almost all of that was sinh(exp(10000)). Before this branch exp(10000) was +oo, so the slow work was never reached. Now it is a finite 8.8e4342, and:

  • e to a huge power. PeterO's exponential works its way to the overflow through a power of e as large as the argument, and ExpDifference called it in a working context with no exponent range. Past ln 10 times the context's ends, e^x is now +oo or 0, decided from x's size alone. Inside ExpDifference, e^v (e^d - 1) is e^(v+d) or -e^v once d is past them.
  • Special functions at a huge argument. A battery at ±10^5000, ±exp(10000), 10^5000 i and 10^5000 + i found 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 past 10^1000 as the infinity it was, so their answers there are master's.
  • Complex paths that lost their digits. The complex paths of exp, sin/cos/tan/cot and arcsin/arccos dropped digits past the reach to their residual scales: sin(10^5000 + i) had no imaginary part, arcsin(10^5000) was 0, and tanh(10^5000 i) had a real part of -0.89. The same reach applies to them, so they are again master's NaN / π/2 - ∞i.
  • e to a complex power goes through Exp, as e to a real power already did. 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 (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:

master branch
limitcheck (twice each) 27.7 s, 28.0 s 28.3 s, 28.1 s
sinh(exp(10000)) 129 ms 131 ms (38,604 ms before this commit)
tanh(exp(10000)) 133 ms 132 ms (19.4 s before)
sinh(10^5000) 135 ms 136 ms (50.7 s before)
erf(10^5000) 132 ms 132 ms (over 20 s before)
cos(sinh(10000)) 130 ms, NaN 384 ms, a correct value
limit sinh(exp(x)) at +oo 170 ms 174 ms
limit sgn(erf(x)), log(erf(x), 2x) at ±oo 120–207 ms 121–211 ms

limitcheck now 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, and PerformanceGate passes. 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 (SolveHard 9,185,040 → 9,186,408 B, SimplifyHard 134,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 - k of them for an argument near 10^k. With the exponents widened, cos(exp(10000)) (NaN before) came back 0.31, where it is 0.92. The same defect was already on master for any decimal past about 10^84: cos(exp(300)) was 0.23 where it is -0.98.

SinAndCos is now NaN for a decimal past 10^(precision - 16). An exact rational keeps the exact reduction, at any size. Every complex path goes through SinAndCos: e to an imaginary power, the polar form of a power, and complex sin/cos/tan/cot. Si of such a decimal is ±π/2, and Ei far up the imaginary axis is ±iπ, where the oscillating part is below the working digits. Changed answers are in BREAKING-CHANGES.md.

.Evaled master branch mpmath
cos(sinh(10000)) NaN NaN 0.97891
cos(exp(10000)) NaN NaN 0.91652
cos(10^150 + 1/3) -0.46528 0.0181629167167… 0.0181629167167…
cos(10^150/3) -0.44819 0.808819030939… 0.808819030939…
cos(exp(2000)) 0.6607 NaN 0.91708
cos(exp(300)) 0.2321 NaN -0.98403
cos(exp(250)) -0.0564 NaN 0.53002
sin(exp(300)) 0.9727 NaN 0.17801
cos(sinh(300)) 0.7849 NaN 0.089364
cos(sqrt(2)*10^200) -0.7623 NaN 0.018225
cos(pi*10^200) -0.9934 NaN 1.0
tan(exp(500)) -0.5548 NaN 0.52510
sin(exp(190)) (just inside the bound) — -0.90939905978336750 (17 digits right) -0.909399059783367503857
Si(exp(300)) π/2 π/2 π/2
Ei(i exp(300)) iπ iπ iπ

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

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

codecov Bot commented Nov 30, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 93.75000% with 8 lines in your changes missing coverage. Please review.
✅ Project coverage is 86.63%. Comparing base (97f26c6) to head (0198f13).
⚠️ Report is 1 commits behind head on master.

Files with missing lines Patch % Lines
...uriMath/Core/Entity/Continuous/Number/Operators.cs 91.11% 1 Missing and 3 partials ⚠️
...nuous/Evaluation.Continuous.Arithmetics.Classes.cs 33.33% 1 Missing and 1 partial ⚠️
...e/Entity/Continuous/Number/ExponentialIntegrals.cs 83.33% 0 Missing and 1 partial ⚠️
...Entity/Continuous/Number/TrigonometricIntegrals.cs 75.00% 0 Missing and 1 partial ⚠️
Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #2674      +/-   ##
==========================================
- Coverage   86.73%   86.63%   -0.10%     
==========================================
  Files         346      346              
  Lines       75913    76133     +220     
  Branches    22231    22314      +83     
==========================================
+ Hits        65841    65959     +118     
- Misses       5426     5504      +78     
- Partials     4646     4670      +24     
Flag Coverage Δ
cpp 37.77% <ø> (ø)
csharp 87.13% <93.75%> (-0.10%) ⬇️
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 Δ
Sources/AngouriMath/Convenience/MathS.cs 91.52% <100.00%> (ø)
...th/Core/Entity/Continuous/Number/ErrorFunctions.cs 95.48% <100.00%> (+0.10%) ⬆️
...uriMath/Core/Entity/Continuous/Number/Polygamma.cs 96.68% <100.00%> (+0.03%) ⬆️
.../AngouriMath/Core/Entity/Continuous/Number/Zeta.cs 85.71% <100.00%> (+1.47%) ⬆️
...Continuous/Integration/IndefiniteIntegralSolver.cs 87.06% <100.00%> (+<0.01%) ⬆️
...rces/AngouriMath/Functions/InternalAMExtensions.cs 85.80% <100.00%> (+0.78%) ⬆️
...e/Entity/Continuous/Number/ExponentialIntegrals.cs 93.75% <83.33%> (-0.59%) ⬇️
...Entity/Continuous/Number/TrigonometricIntegrals.cs 93.54% <75.00%> (-0.84%) ⬇️
...nuous/Evaluation.Continuous.Arithmetics.Classes.cs 89.01% <33.33%> (-0.16%) ⬇️
...uriMath/Core/Entity/Continuous/Number/Operators.cs 89.03% <91.11%> (-2.59%) ⬇️

... and 7 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 and others added 2 commits November 30, 2026 20:47
…' 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>
@Rafael-SOWNet
Rafael-SOWNet merged commit 4674e66 into master Dec 1, 2026
35 checks passed
@github-project-automation github-project-automation Bot moved this from Todo to Done in AngouriMath Dec 1, 2026
@Rafael-SOWNet
Rafael-SOWNet deleted the exact-evaluation-below-the-floor-2668 branch December 1, 2026 01:04
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.

An exact expression whose parts are below 10^-100 evaluates to wrong digits: sqrt(1/10^200 + 1/10^210) * 10^100 is 0

1 participant