Skip to content

Order exact numbers exactly, and leave a comparison the decimals cannot decide (#2664) - #2665

Open
Rafael-SOWNet wants to merge 1 commit into
masterfrom
exact-comparisons-2664
Open

Rafael-SOWNet wants to merge 1 commit into
masterfrom
exact-comparisons-2664

Conversation

@Rafael-SOWNet

Copy link
Copy Markdown
Member

Comparisons of evaluated numbers went through their hundred-digit decimals, so they gave wrong True/False answers at extreme magnitudes and for close values.

Fix, inside the comparison code (Evaluation.Discrete.Classes.cs):

  • Exact rationals: two rationals are ordered exactly.
  • Decimals: a decimal decides an order only where the two sides differ by more than the downcasting tolerance relative to the larger. Where they differ by less, an equality is declined rather than answered False.
  • Flushed or saturated sides: a side written as a product, quotient or power of nonzero finite constants that evaluates to 0 or to an infinity has been flushed below the decimal range or saturated above it, so it decides nothing.

In each undecided case the comparison stays written. This is a temporary reading before v3, linked to #1019 §10.

.Evaled 2.10.0 / master this branch truth
10^(-200) < 2 * 10^(-200) False True True
10^(-300) > 0 False True True
10^(2360) > 10^(1175) False True True
-10^(400) > -10^(400) - 1 False True True
1000! * 2 < 1001! False True True
10^(590) >= 10^(590) + 10^(440) True False False
exp(-1000.5) > exp(-3002.5) False stays written True
exp(6000) >= exp(6100) True stays written False
exp(-2000) = exp(-2000.5) True stays written False
sqrt(5)^1000 = 5^500 False stays written True
sqrt(2) < sqrt(2) + 10^(-150) False stays written True

Sweep. Two runs of 260 comparisons (<, >, <=, >=, =) between exact values from 1e-3000 to 1e3000, including pairs that differ by a tiny relative amount. Truth comes from exact rational arithmetic, or from mpmath at 400 digits for exp and sqrt.

master this branch
wrong 83 and 84 1 and 1
declined 0 98 and 76

The one remaining wrong answer in each run is sqrt(2) = sqrt(2) + 10^(-99) → True. That is a residual that cannot be told apart from a difference, and it is #1376's agreed v3 question, so this PR does not touch it.

Relation to #2650. This does not depend on #2650 (exact-verify-2650). The flush check is structural and runs in the comparison code before the exact 0 a flushed value is downcast to is read. It does not touch ExpandOnTwoArguments, CannotBeZero or SignOfANonzero.

Checks.

  • New test: 17 cases.
  • Full UnitTests suite: 21901 passed, 0 failed, 13 skipped (net10.0).
  • The library builds with 0 errors for every target framework.

Closes #2664.

🤖 Generated with Claude Code

…ot decide (#2664)

The ordering arms of Greaterf, Lessf, GreaterOrEqualf and LessOrEqualf
compared two evaluated numbers through their hundred-digit decimals.
10^(-200) < 2 * 10^(-200) was False, because both sides fall below the
decimals' least exponent. 10^2360 > 10^1175 was False, because both
overflow. -10^400 > -10^400 - 1 and 1000! * 2 < 1001! were False,
because each pair rounds to one decimal. Equality answered
exp(-2000) = exp(-2000.5) with True, both sides flushed to 0, and
sqrt(5)^1000 = 5^500 with False, the decimal differing from the exact
integer in its last digits.

Two rationals are now ordered exactly. A decimal decides an order only
where the two sides differ by more than the downcasting tolerance
relative to the larger, and an equality is declined, not answered
False, where they differ by less. A side written as a product, quotient
or power of nonzero finite constants that evaluates to 0 or to an
infinity was flushed or saturated, and decides nothing. In each of these
cases the comparison stays written. This is checked in the comparison
code itself, before the exact 0 a flushed value is downcast to is read,
so it does not depend on how Evaluation.Classes reads such a value.
Two decimals that agree are still equal (#1376). This is a temporary
reading before v3, which decides exact comparisons exactly.

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

codecov Bot commented Nov 29, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 88.23529% with 6 lines in your changes missing coverage. Please review.
✅ Project coverage is 86.70%. Comparing base (cc86a04) to head (bd65d9d).

Files with missing lines Patch % Lines
...Evaluation.Discrete/Evaluation.Discrete.Classes.cs 88.23% 1 Missing and 5 partials ⚠️
Additional details and impacted files

Impacted file tree graph

@@           Coverage Diff           @@
##           master    #2665   +/-   ##
=======================================
  Coverage   86.70%   86.70%           
=======================================
  Files         346      346           
  Lines       75739    75785   +46     
  Branches    22152    22176   +24     
=======================================
+ Hits        65671    65713   +42     
  Misses       5431     5431           
- Partials     4637     4641    +4     
Flag Coverage Δ
cpp 37.77% <ø> (ø)
csharp 87.20% <88.23%> (+<0.01%) ⬆️
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 Δ
...Evaluation.Discrete/Evaluation.Discrete.Classes.cs 90.14% <88.23%> (-0.11%) ⬇️

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

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.

A comparison of exact numbers goes through decimals: 10^(-200) < 2 * 10^(-200) is False

1 participant