Skip to content

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

Open
Rafael-SOWNet wants to merge 1 commit into
masterfrom
exact-evaluation-below-the-floor-2668
Open

Rafael-SOWNet wants to merge 1 commit into
masterfrom
exact-evaluation-below-the-floor-2668

Conversation

@Rafael-SOWNet

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

C# suite: 21,981 passed, 0 failed, 13 skipped. The F# suite, the harnesses against master, and the performance comparison are in progress, and their numbers will be added 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

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 86.66%. Comparing base (97f26c6) to head (8489e6c).

Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #2674      +/-   ##
==========================================
- Coverage   86.73%   86.66%   -0.07%     
==========================================
  Files         346      346              
  Lines       75913    75966      +53     
  Branches    22231    22245      +14     
==========================================
- Hits        65841    65836       -5     
- Misses       5426     5477      +51     
- Partials     4646     4653       +7     
Flag Coverage Δ
cpp 37.77% <ø> (ø)
csharp 87.16% <100.00%> (-0.07%) ⬇️
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%> (ø)
...uriMath/Core/Entity/Continuous/Number/Operators.cs 89.13% <100.00%> (-2.49%) ⬇️
...Continuous/Integration/IndefiniteIntegralSolver.cs 87.06% <100.00%> (+<0.01%) ⬆️
...rces/AngouriMath/Functions/InternalAMExtensions.cs 85.97% <100.00%> (+0.94%) ⬆️

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

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Todo

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