Repository navigation
Decide a finite set's membership by value where the decimal key misses (#2755) - #2772
Open
Rafael-SOWNet wants to merge 2 commits into
Open
Rafael-SOWNet wants to merge 2 commits into
Rafael-SOWNet wants to merge 2 commits into
Conversation
A finite set keys its members by their hundred-digit evaluations, and one
number written two ways can evaluate to decimals that differ in the last
digit: sqrt(8) ends ...283145, 2 sqrt(2) ends ...283146. TryContains took
the miss for "not a member" whenever both sides were constant, so
sqrt(8) in { 2 sqrt(2) } was False, the set { (1 + sqrt(2))^2, 3 + 2 sqrt(2) }
kept the number twice, and { (1 + sqrt(2))^2 } \ { 3 + 2 sqrt(2) } kept it.
On a miss, members whose evaluations are within the downcasting tolerance
of the element are compared as numbers: the same where their difference
simplifies to 0, different where enclosures in twice the working digits
are apart or the difference is a nonzero rational, and otherwise not
decided -- which TryContains reports as undecided rather than false. A
member shown the same decides membership even beside symbolic members. The
set's construction merges members shown the same, the difference of two
finite sets takes a respelled member out, and a set as an element is
compared with set members by mutual inclusion.
Evaluated, the members are decimals, which decide nothing at that
distance, as an equality of two near decimals does not: `in` asks the
written set instead, and a set operation on two evaluated sets stays
written where it returned a wrong set.
Equality of FiniteSet entities keeps comparing keys, as before, so that
entity equality never simplifies.
Measured: 600 generated respellings (radicals, rationalised and nested
forms, trigonometric and logarithmic values, complex roots, and perturbed
non-members, truth by mpmath at 200 digits) asked through TryContains,
Evaled and Simplify against sets of numbers and with a symbolic member:
master 398 / 157 wrong / 45 declined for TryContains and Evaled and
430 / 157 / 13 for Simplify; branch 600 right of 600 in each, no row worse.
Fixes #2755
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Math.Clamp and Math.ScaleB are not in netstandard2.0. The clamp is Min/Max; 2^shift is written into the exponent bits of a double, which is exactly 2^shift for the shifts used (0 to 1023), so the product with the leading 62 bits of the mantissa is the same as ScaleB's. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Codecov Report❌ Patch coverage is Additional details and impacted files@@ Coverage Diff @@
## master #2772 +/- ##
==========================================
+ Coverage 86.70% 86.72% +0.02%
==========================================
Files 346 347 +1
Lines 77113 77245 +132
Branches 22699 22753 +54
==========================================
+ Hits 66859 66990 +131
+ Misses 5533 5526 -7
- Partials 4721 4729 +8
Flags with carried forward coverage won't be shown. Click here to find out more.
🚀 New features to boost your workflow:
|
Rafael-SOWNet
added a commit
that referenced
this pull request
Dec 6, 2026
…ce that evaluates to 0 SetOperators.Order took two values within the downcasting tolerance as equal where (a - b).Evaled was an exact 0, and the number factory rounds any residual below half the working digits to 0: pi + 10^(-60) in [0; pi] was decided yes, pi - 10^(-60) in (0; pi) no. The tie now goes to FiniteSet.SameNumber (#2772): equal only where the difference simplifies to 0, undecided where nothing shows either, and where the two are apart their order comes from enclosures in twice the working digits, or from a rational difference, and is otherwise undecided. The FiniteSet membership loop of the first commit is dropped for #2772's. Measured on the sweep with near-equal perturbations (10^-3, 10^-20, 10^-60, 10^-90 on both sides of ends and members), 7900 cases: master 1468 wrong, this branch 4, all a listed set less a listed set with a symbolic member (TryFullSubtract), as before. Part of #2756 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 #2755
Cause
FiniteSetkeys its members by their hundred-digit evaluations. One number written two ways can evaluate to decimals that differ in the last digit:sqrt(8)ends...283145and2 sqrt(2)ends...283146. When the lookup missed and both sides were constant,TryContainsanswered "decided: not a member". The same miss also kept a number twice when the set was built, and left it in place in{ A } \ { B }.Change
TryContains. On a miss, the members whose evaluations are within the downcasting tolerance of the element are compared as numbers (FiniteSet.SameNumber, new fileSets/FiniteSet.SameNumber.cs):PreciseEvaluation) do not overlap, or the difference simplifies to a nonzero rational;(a - b).Simplify()is the integer 0;A member shown to be the same decides membership even when symbolic members are present (
sqrt(8) in { x, 2 sqrt(2) }). A set given as the element is compared with set members by mutual inclusion.Construction merges members that are shown to be the same. Two cheap gates keep this off the common path: a set with only exact keys returns straight away, and a double sieve stops anything not near from reaching the decimal comparison.
Difference of finite sets. A respelled member is removed. Where the comparison is undecided, the difference is left as written.
Evaluation. An evaluated member is a decimal, and two decimals that agree this far decide nothing (the same rule
Equalsfapplies). Soinasks the written set instead, and a set operation on two evaluated sets stays written where it used to return a wrong set.FiniteSet.Equals(entity equality) still compares keys, as before. Entity equality never simplifies.Before / after (measured on probes at
578680eedand at this branch)TryContains:sqrt(8)in{ 2 sqrt(2) },1/sqrt(2)in{ sqrt(2)/2 },sin(pi/3)in{ sqrt(3)/2 },(1+sqrt(2))^2in{ 3 + 2 sqrt(2) },1/2 + sqrt(5)/2in{ (1+sqrt(5))/2 },sqrt(-8)in{ 2 sqrt(2) i },cos(pi/5)in{ (1+sqrt(5))/4 },sqrt(2+sqrt(3))in{ (sqrt(6)+sqrt(2))/2 }sqrt(8)in{ x, 2 sqrt(2) }{ sqrt(8) }in{ { 2 sqrt(2) } }(sqrt(8) in { 2 sqrt(2) }): Evaled / SimplifyFalse/FalseTrue/True({ sin(pi/3) } = { sqrt(3)/2 }).Simplify()FalseTrue{ (1+sqrt(2))^2, 3 + 2 sqrt(2) }.Simplify(){ 3 + 2 * sqrt(2) }({ (1+sqrt(2))^2 } \ { 3 + 2 sqrt(2) }).Simplify(){ (1 + sqrt(2)) ^ 2 }{ }({ (1+sqrt(2))^2 } /\ { 3 + 2 sqrt(2) }).Simplify(){ }{ (1 + sqrt(2)) ^ 2 }({ sqrt(8) } \ { 2 sqrt(2) }).Evaled{ 2.8284...145 }pi + 10^(-60)in{ pi },pi*10^(-80)in{ e*10^(-80) },3in{ 1, 2 },xin{ 1, 2 }pi + 10^(-120) in { pi }is still decided yes. The two numbers agree in all 100 digits and share a key, which is #1376 (v3).BREAKING-CHANGES.mdhas an entry.Sweep
600 generated pairs, with truth from mpmath at 200 digits. The respellings cover radicals, rationalised forms, nested radicals, expanded and factored squares, trigonometric values, logarithms and complex roots. The near non-members are the same numbers perturbed by negation, doubling, adding 1/k, or adding 10^-3, 10^-20, 10^-60 or 10^-90. Each pair is asked against sets of numbers and against sets that include a symbolic member, through
TryContains,EvaledandSimplify:TryContains/EvaledSimplifyNo row got worse. A sample of 106
solve::rows gives identical answers on master and on the branch.Suite and harnesses
Timing
Building a
FiniteSetof irrational constants costs about 0.5 to 1 µs more per set. Measured over 200,000 members: 60 to 85 ms on master against 64 to 260 ms on the branch, and the spread is noisy. A set whose members are all exact is unaffected. The 106solve::rows took 2917 ms on master and 2997 ms on the branch (one run each).With #2769
#2769 also adds a by-value fallback in
FiniteSet.TryContains, throughSetOperators.Order. The two branches now conflict in that one hunk, which is deliberate: merged textually without a conflict, #2769's loop runs first and decidespi + 10^(-60) in { pi }as yes. The resolution is to keep this branch's hunk and drop #2769'sFiniteSetloop. #2769's interval changes are untouched.🤖 Generated with Claude Code