Skip to content

Verify an exact root far below 1 exactly, not through flushed decimals - #2669

Open
Rafael-SOWNet wants to merge 1 commit into
masterfrom
exact-verify-2650
Open

Rafael-SOWNet wants to merge 1 commit into
masterfrom
exact-verify-2650

Conversation

@Rafael-SOWNet

Copy link
Copy Markdown
Member

Closes #2650

An equation that is solved with its roots near 1 was declined once x was written as 10^30 x. Evaluation's decimals stop at about 10^-100 and flush anything smaller to 0, and several places read that flushed value as exact:

  • ExpandOnTwoArguments (Evaluation.Classes.cs) took an exact Evaled for the node's value. A root, power or quotient of exact rationals that had flushed counted as well, so sqrt(5/10^200 - 4/10^200) became 0, and the quartic formula's -Q/2 + sqrt(Q^2/4 + P^3/27) became -Q/2. CannotBeZero and SignOfANonzero read rationals through the 1e-16 IsZero.
    • A rational now counts as zero only when it is 0.
    • A sum or product of rationals whose parts don't decide its sign takes the sign from its exact value.
    • The shortcut declines where a part made only of exact numbers cannot be 0 yet evaluated to 0. A part with a decimal in it folds as before.
  • The cubic and quartic formulas (PolynomialSolver.cs) chose a branch by Evaled == 0, which is within 10^-6. They now compare the part with the terms it is made of: exactly when it is rational, and otherwise relative to those terms. Where Q is exactly 0, the quartic takes t = 0 as its root of the resolvent, instead of U - P/(3U), which divides by a U near 10^-60.
  • The residual checks evaluated with the floor, so a true root's residual came out as 0.087 at any precision. They now evaluate without it:
  • Two zero tests were absolute below 1. ModulusSolver.IsClearOfZero measured against max(1, |re|), and Groebner IsNegligible against max(1, largest term). Both now measure against the value's own size.

A residual that is not shown to be zero still declines the candidate. No tolerance was loosened.

Rebased on f8a822d, so on top of #2644 and #2649.

Answers

scalecheck.py with x:K on the 200 cxcheck equations and the radcheck and logcheck corpora. Each cell counts the equations whose scaled answer differs from the unscaled one, master f8a822d against this branch:

10^-30 10^30 10^-20 10^20 10^-12 10^12
cxcheck, master 15 45 7 12 9 0
cxcheck, this 4 12 7 3 9 0
radcheck, master / this 0 / 0 0 / 0 0 / 0 0 / 0 0 / 0 0 / 0
logcheck, master / this 1 / 1 0 / 0 1 / 1 0 / 0 0 / 0 0 / 0
  • Regressions. No equation differs here that doesn't differ on master. Every one still differing is a decline.
  • mpmath check of the 53 newly agreeing rows:
    • Each answer with roots was re-solved and evaluated at 120 digits. Every residual is at most 3e-98, with the scaled variable of order 1.
    • The 9 rows answered with no roots: a Newton search from 300 starts finds no root for any of them.
  • How the roots were read. Both arms read the roots without the exponent floor. The stock runner reads them with EvalNumerical() in the default context, which flushes the parts of an exact root near 10^-20 and reports right answers as wrong roots. For example, (-2i + 3y) + (-2 + 3y) abs(-3y) = 0 at y = 10^20 x is answered with the root mpmath puts at 0.393078192784 + 0.273588473883i (residual 2e-299). Read through EvalNumerical, it comes out as 0.262657 + 0.404009i. That 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, which is on master and 2.10.0 alike: sqrt(1/10^200 + 1/10^210) * 10^100 evaluates to 0.

Performance

The Kernel benchmark, CommonFunctionsInterVersion, on one machine and in one session. The arms alternate: master, this branch, master, this branch.

master, time this, time master, allocated this, allocated
SimplifyEasy 28.2 / 29.7 µs 29.8 / 29.5 µs 28,121 B 28,121 B
SimplifyHard 77.6 / 78.7 ms 77.5 / 77.6 ms 134,376,928 B 134,559,688 B (+0.14%)
Derivate 15.6 / 15.2 µs 16.2 / 15.6 µs 53,914 B 54,114 B (+0.37%)
SolveEasy 215.0 / 212.2 µs 215.0 / 213.5 µs 419,515 B 419,515 B
SolveMedium 276.4 / 279.5 µs 296.4 / 281.9 µs 452,181 B 456,045 B (+0.85%)
SolveMediumHard 9.03 / 9.17 ms 9.06 / 9.17 ms 1,404,433 B 1,408,777 B (+0.31%)
SolveHard 69.5 / 67.3 ms 67.0 / 65.4 ms 9,178,536 B 9,183,904 B (+0.06%)
EvalTrig, EvalTrigPrecise, Eval*Fresh equal equal equal equal

The first version of this branch set the unbounded context on every Weigh call. Setting a setting starts a new cache state, so SolveEasy allocated 33% more (556,893 B) and ran 30% slower. The tree walk allocated a stack on every exact shortcut. Weigh now evaluates without the floor only after the floored reading goes against the root, and the walk is recursive and allocates nothing.

The suite passes: 21872, 0 failed. The library builds for every target framework.

Two related findings are filed separately and not fixed here:

🤖 Generated with Claude Code

Evaluation's decimals stop at about 10^-100 and flush anything smaller to
0. Four places read that flushed value as exact, so an equation of x with
roots near 10^-30 stayed unsolved where the same equation in 10^30 x was
solved:

- ExpandOnTwoArguments took an exact Evaled for the node's value. A
  product, quotient or power of exact rationals that flushed counted, so
  sqrt(5/10^200 - 4/10^200) became 0, and the quartic formula's
  -Q/2 + sqrt(Q^2/4 + P^3/27) became -Q/2. CannotBeZero and
  SignOfANonzero read rationals through the 1e-16 IsZero. A rational now
  counts as zero only when it is 0, a sum or product of rationals whose
  parts do not decide its sign takes it from its exact value, and the
  shortcut, through an allocation-free walk, declines when a
  part made only of exact numbers cannot be 0 but evaluated to 0. A part
  with a decimal in it, such as a Newton root, folds as before.
- The cubic and quartic formulas chose a branch by Evaled == 0, which
  uses a 1e-6 tolerance. They now compare the part with the terms it is
  made of: exactly when the part is rational, and otherwise relative to
  those terms. Where Q is exactly 0, the quartic takes t = 0 as its root
  of the resolvent t^3 + P t + Q, instead of U - P/(3U), which is 0 too
  but divides by a U that evaluates to 0 near 10^-60.
- StatementSolver.Weigh, Groebner Satisfies and ModulusSolver's residual
  evaluated with the floor, so a true root's residual came out as 0.087
  at any precision. They now evaluate without the exponent floor: Weigh
  and the as-written check only where the floored reading is against the
  root, since setting a setting starts a new cache state and doing it on
  every call was a third more bytes on solving x^2 + x + 1; Satisfies on
  every candidate, as a flushed residual also passes a wrong row. So do
  the decimal entries of a Groebner solution, which were evaluated back
  outside it to a row that was no solution. A residual not shown to be
  zero still declines the candidate. Satisfies measured a decimal
  residual against max(1, largest term), which is absolute below 1, and
  now measures it against the largest term alone. The check that judges
  a decimal root against the terms as written evaluates without the
  floor where its floored reading drops the root.
- ModulusSolver.IsClearOfZero measured an imaginary part against
  max(1, |re|), which is absolute below 1. TryReadReal now measures it
  against the size of the number.

#2650

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@codecov

codecov Bot commented Nov 29, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 86.70%. Comparing base (f8a822d) to head (c46e6ba).
⚠️ Report is 3 commits behind head on master.

Additional details and impacted files

Impacted file tree graph

@@           Coverage Diff           @@
##           master    #2669   +/-   ##
=======================================
  Coverage   86.70%   86.70%           
=======================================
  Files         346      346           
  Lines       75724    75712   -12     
  Branches    22145    22140    -5     
=======================================
- Hits        65654    65644   -10     
+ Misses       5431     5430    -1     
+ Partials     4639     4638    -1     
Flag Coverage Δ
cpp 37.77% <ø> (ø)
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 Δ
...Functions/Algebra/Groebner/GroebnerSystemSolver.cs 82.69% <ø> (+0.30%) ⬆️
...Continuous/Solvers/EquationSolver/ModulusSolver.cs 79.23% <ø> (-0.18%) ⬇️
...tinuous/Solvers/EquationSolver/PolynomialSolver.cs 86.59% <ø> (+0.16%) ⬆️
...ontinuous/Solvers/EquationSolver/SolveStatement.cs 83.52% <ø> (ø)
...uriMath/Functions/Evaluation/Evaluation.Classes.cs 85.44% <ø> (-0.21%) ⬇️
🚀 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 exactly solvable equation is declined when its roots are near 10^-30: the decimals' exponent floor flushes parts of exact candidates to 0

1 participant