From bd65d9dcffe858118c5ac46ebb873a586cc7cf3e Mon Sep 17 00:00:00 2001 From: Rafael Vuijk Date: Sun, 29 Nov 2026 14:33:49 +0000 Subject: [PATCH] Order exact numbers exactly, and leave a comparison the decimals cannot 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 --- BREAKING-CHANGES.md | 21 ++++ .../Evaluation.Discrete.Classes.cs | 104 +++++++++++++++++- .../AComparisonOfExactNumbersIsExactTest.cs | 50 +++++++++ 3 files changed, 170 insertions(+), 5 deletions(-) create mode 100644 Sources/Tests/UnitTests/Common/AComparisonOfExactNumbersIsExactTest.cs diff --git a/BREAKING-CHANGES.md b/BREAKING-CHANGES.md index 4a85794fe..28372163b 100644 --- a/BREAKING-CHANGES.md +++ b/BREAKING-CHANGES.md @@ -67,6 +67,27 @@ integer. Exact large integers are unchanged | `"integral(exp(x), x, 0, 300)".Simplify()` | `1942426395…4199999…9` | `e ^ 300 - 1` | | `"2^340 * sqrt(2)".Evaled` | an `Integer` | `3.167477390…E+102`, a `Real` | +### A comparison of exact numbers is exact, and one the decimals cannot decide stays written + +**Not silent: wrong `True` and `False` answers change.** A comparison of two evaluated numbers went through their +hundred-digit decimals. So `10^(-200) < 2 * 10^(-200)` was `False`, both sides being below the decimals' least +exponent, and `1000! * 2 < 1001!` was `False`, the two sides rounding to one decimal. Two rationals are now ordered +exactly. A decimal decides an order or an inequality only where the two sides differ by more than the downcasting +tolerance relative to the larger. A side written as a product or a power of nonzero finite constants that evaluates to +0 or to an infinity was flushed or saturated, and decides nothing. Such a comparison stays written. This is a reading +before v3, which decides exact comparisons exactly (#1019 §10). Two decimals that agree are still equal, which is +#1376 ([#2664](https://github.com/asc-community/AngouriMath/issues/2664)). + +| `.Evaled` | Was (2.10.0) | Now | +|---|---|---| +| `10^(-200) < 2 * 10^(-200)` | `False` | `True` | +| `1000! * 2 < 1001!` | `False` | `True` | +| `10^(590) >= 10^(590) + 10^(440)` | `True` | `False` | +| `exp(-1000.5) > exp(-3002.5)` | `False` | stays written | +| `exp(-2000) = exp(-2000.5)` | `True` | stays written | +| `sqrt(5)^1000 = 5^500` | `False` | stays written | +| `sqrt(2) < sqrt(2) + 10^(-150)` | `False` | stays written | + ### A square system is answered exactly whichever order its equations are in **Not silent: a decimal answer is now exact, and a refused system has an answer.** The eliminator took the last diff --git a/Sources/AngouriMath/Functions/Evaluation/Evaluation.Discrete/Evaluation.Discrete.Classes.cs b/Sources/AngouriMath/Functions/Evaluation/Evaluation.Discrete/Evaluation.Discrete.Classes.cs index df27868e6..07ea2739a 100644 --- a/Sources/AngouriMath/Functions/Evaluation/Evaluation.Discrete/Evaluation.Discrete.Classes.cs +++ b/Sources/AngouriMath/Functions/Evaluation/Evaluation.Discrete/Evaluation.Discrete.Classes.cs @@ -322,7 +322,8 @@ private static Entity MatricesAreEqual(Matrix left, Matrix right, bool isExact) /// protected override Entity InnerSimplify(bool isExact) => ExpandOnTwoArguments(Left, Right, - (left, right) => left == right ? true + (left, right) => EqualityIsUndecided(Left, Right, left, right) ? null + : left == right ? true : left is Matrix matrixLeft && right is Matrix matrixRight ? MatricesAreEqual(matrixLeft, matrixRight, isExact) // A size that is not a number is compared as a size, never by the difference // of two expressions: card(ZZ) - card(QQ) is not zero, and the two are equal. @@ -341,6 +342,95 @@ protected override Entity InnerSimplify(bool isExact) propagateSet: !(Left.InnerSimplified(isExact) is Set && Right.InnerSimplified(isExact) is Set)); } + /// + /// The sign of less , the values of + /// and , where it is decided; or + /// . + /// + /// + /// Two rationals are ordered exactly. Through their hundred-digit decimals, 10^(-200) < 2 * 10^(-200) + /// was False, both being below the decimals' least exponent, and so were -10^(400) > -10^(400) - 1 and + /// 1000! * 2 < 1001!, the two sides rounding to one decimal. A decimal decides an order only where the two + /// sides differ by more than the downcasting tolerance relative to the larger, the digits below that being + /// rounding: sqrt(2) < sqrt(2) + 10^(-150) was False. A side written as a product or a power of + /// nonzero finite constants that evaluates to 0 or to an infinity was flushed below the decimals' range or + /// saturated above it, and says nothing of the order: exp(-1000.5) > exp(-3002.5) was False and + /// exp(6000) >= exp(6100) True. Left undecided, the comparison stays written. A temporary reading + /// before v3, which decides an exact comparison exactly and an approximate one to a requested accuracy: + /// https://github.com/asc-community/AngouriMath/issues/1019#issuecomment-5754734225 + /// https://github.com/asc-community/AngouriMath/issues/2664 + /// + private static int? Ordered(Entity leftWritten, Entity rightWritten, Number.Real left, Number.Real right) + { + if (leftWritten == rightWritten) + return 0; + // First, since a flushed value comes back as the exact 0. + if (IsFlushedOrSaturated(leftWritten, left) || IsFlushedOrSaturated(rightWritten, right)) + return null; + if (left is Number.Rational { ERational: var exactLeft } && right is Number.Rational { ERational: var exactRight }) + return exactLeft.CompareTo(exactRight); + var (a, b) = (left.EDecimal, right.EDecimal); + if (a.IsNaN() || b.IsNaN()) + return null; + if (!a.IsFinite || !b.IsFinite) + return a.CompareTo(b); + var difference = a.Subtract(b); + var larger = a.Abs().CompareTo(b.Abs()) >= 0 ? a.Abs() : b.Abs(); + if (difference.Abs().CompareTo(MathS.Settings.DowncastingTolerance.Multiply(larger)) <= 0) + return null; + return difference.Sign; + } + + /// + /// Whether an equality of two evaluated numbers is not decided by them: a side flushed or saturated (see + /// ), or two decimals that differ by no more than the downcasting tolerance relative to the + /// larger, which is rounding. exp(-2000) = exp(-2000.5) was True, both flushed to 0, and + /// sqrt(5)^1000 = 5^500 False, the decimal differing from the exact integer in its last digits. + /// Two decimals that agree are equal as before: that a residual and a difference look alike is #1376. + /// https://github.com/asc-community/AngouriMath/issues/1019#issuecomment-5754734225 + /// https://github.com/asc-community/AngouriMath/issues/2664 + /// + private static bool EqualityIsUndecided(Entity leftWritten, Entity rightWritten, Entity left, Entity right) + { + if (left is not Number.Real l || right is not Number.Real r || leftWritten == rightWritten) + return false; + if (IsFlushedOrSaturated(leftWritten, l) || IsFlushedOrSaturated(rightWritten, r)) + return true; + if (l is Number.Rational && r is Number.Rational) + return false; + var (a, b) = (l.EDecimal, r.EDecimal); + if (!a.IsFinite || !b.IsFinite || a.CompareTo(b) == 0) + return false; + var larger = a.Abs().CompareTo(b.Abs()) >= 0 ? a.Abs() : b.Abs(); + return a.Subtract(b).Abs().CompareTo(MathS.Settings.DowncastingTolerance.Multiply(larger)) <= 0; + } + + /// + /// Whether , 0 or an infinity, is what the decimals made of , a + /// product, a quotient or a power of nonzero finite constants, which is neither. + /// + private static bool IsFlushedOrSaturated(Entity written, Number.Real value) + { + // A rational is read by its exact value: through its decimal, 10^(-200) is 0 and 10^2400 an infinity. An exact + // 0 is what a flushed value is downcast to. + var madeByTheDecimals = value is Number.Rational { ERational: var exact } + ? exact.IsZero + : value.EDecimal.IsZero || !value.EDecimal.IsFinite; + return madeByTheDecimals && IsNonzeroAndFinite(written); + + // A product, a quotient or a power of nonzero finite constants: never 0, never infinite. + static bool IsNonzeroAndFinite(Entity expr) => expr switch + { + Number.Complex number => !number.IsZero && Number.PartsAreFinite(number), + Variable { IsConstant: true } constant => constant.Evaled is Number.Complex { IsZero: false } value && Number.PartsAreFinite(value), + Mulf(var a, var b) => IsNonzeroAndFinite(a) && IsNonzeroAndFinite(b), + Divf(var a, var b) => IsNonzeroAndFinite(a) && IsNonzeroAndFinite(b), + Powf(var @base, var exponent) => IsNonzeroAndFinite(@base) && !exponent.Vars.Any() + && exponent.Evaled is Number.Real { EDecimal.IsFinite: true }, + _ => false, + }; + } + /// /// as the real it is, or where it has an /// imaginary part. With downcasting off a value such as cos(3) evaluates to a @@ -439,7 +529,8 @@ protected override Entity InnerSimplify(bool isExact) => ExpandOnTwoArguments(Left, Right, (a, b) => (a, b) switch { - (Number numLeft, Number numRight) when AsAReal(numLeft) is { } reLeft && AsAReal(numRight) is { } reRight => reLeft > reRight, + (Number numLeft, Number numRight) when AsAReal(numLeft) is { } reLeft && AsAReal(numRight) is { } reRight + => Ordered(Left, Right, reLeft, reRight) is { } sign ? (Entity)(sign > 0) : null, (Number numLeft, Number numRight) => MathS.NaN, _ when AngouriMath.Core.Sets.Cardinality.IsSize(a) || AngouriMath.Core.Sets.Cardinality.IsSize(b) => AngouriMath.Core.Sets.Cardinality.Compare(a, b, static sign => sign > 0, isExact), _ when OrderOfUnbuiltPowers(a, b) is { } sign => sign > 0, @@ -460,7 +551,8 @@ protected override Entity InnerSimplify(bool isExact) => ExpandOnTwoArguments(Left, Right, (a, b) => (a, b) switch { - (Number numLeft, Number numRight) when AsAReal(numLeft) is { } reLeft && AsAReal(numRight) is { } reRight => reLeft >= reRight, + (Number numLeft, Number numRight) when AsAReal(numLeft) is { } reLeft && AsAReal(numRight) is { } reRight + => Ordered(Left, Right, reLeft, reRight) is { } sign ? (Entity)(sign >= 0) : null, (Number numLeft, Number numRight) => MathS.NaN, _ when AngouriMath.Core.Sets.Cardinality.IsSize(a) || AngouriMath.Core.Sets.Cardinality.IsSize(b) => AngouriMath.Core.Sets.Cardinality.Compare(a, b, static sign => sign >= 0, isExact), _ when OrderOfUnbuiltPowers(a, b) is { } sign => sign >= 0, @@ -481,7 +573,8 @@ protected override Entity InnerSimplify(bool isExact) => ExpandOnTwoArguments(Left, Right, (a, b) => (a, b) switch { - (Number numLeft, Number numRight) when AsAReal(numLeft) is { } reLeft && AsAReal(numRight) is { } reRight => reLeft < reRight, + (Number numLeft, Number numRight) when AsAReal(numLeft) is { } reLeft && AsAReal(numRight) is { } reRight + => Ordered(Left, Right, reLeft, reRight) is { } sign ? (Entity)(sign < 0) : null, (Number numLeft, Number numRight) => MathS.NaN, _ when AngouriMath.Core.Sets.Cardinality.IsSize(a) || AngouriMath.Core.Sets.Cardinality.IsSize(b) => AngouriMath.Core.Sets.Cardinality.Compare(a, b, static sign => sign < 0, isExact), _ when OrderOfUnbuiltPowers(a, b) is { } sign => sign < 0, @@ -501,7 +594,8 @@ protected override Entity InnerSimplify(bool isExact) => ExpandOnTwoArguments(Left, Right, (a, b) => (a, b) switch { - (Number numLeft, Number numRight) when AsAReal(numLeft) is { } reLeft && AsAReal(numRight) is { } reRight => reLeft <= reRight, + (Number numLeft, Number numRight) when AsAReal(numLeft) is { } reLeft && AsAReal(numRight) is { } reRight + => Ordered(Left, Right, reLeft, reRight) is { } sign ? (Entity)(sign <= 0) : null, (Number numLeft, Number numRight) => MathS.NaN, _ when AngouriMath.Core.Sets.Cardinality.IsSize(a) || AngouriMath.Core.Sets.Cardinality.IsSize(b) => AngouriMath.Core.Sets.Cardinality.Compare(a, b, static sign => sign <= 0, isExact), _ when OrderOfUnbuiltPowers(a, b) is { } sign => sign <= 0, diff --git a/Sources/Tests/UnitTests/Common/AComparisonOfExactNumbersIsExactTest.cs b/Sources/Tests/UnitTests/Common/AComparisonOfExactNumbersIsExactTest.cs new file mode 100644 index 000000000..78b3a2412 --- /dev/null +++ b/Sources/Tests/UnitTests/Common/AComparisonOfExactNumbersIsExactTest.cs @@ -0,0 +1,50 @@ +// +// Copyright (c) 2019-2026 Angouri. +// AngouriMath is licensed under MIT. +// Details: https://github.com/asc-community/AngouriMath/blob/master/LICENSE.md. +// Website: https://am.angouri.org. +// + +using AngouriMath.Extensions; +using Xunit; +using static AngouriMath.Entity; + +namespace AngouriMath.Tests.Common +{ + /// + /// A comparison of two evaluated numbers went through their hundred-digit decimals: 10^(-200) < 2 * 10^(-200) + /// was False, both sides below the decimals' least exponent, and 1000! * 2 < 1001! was False, + /// the two rounding to one decimal. Two rationals are ordered exactly; a decimal decides only where it separates the + /// two sides; and a side flushed to 0 or saturated to an infinity decides nothing, so the comparison stays written. + /// https://github.com/asc-community/AngouriMath/issues/2664 + /// + [Trait("Area", "Common")] + public sealed class AComparisonOfExactNumbersIsExactTest + { + [Theory] + [InlineData("10^(-200) < 2 * 10^(-200)", true)] + [InlineData("10^(-300) > 0", true)] + [InlineData("10^(-1490) <= 0", false)] + [InlineData("10^(2360) > 10^(1175)", true)] + [InlineData("-10^(400) > -10^(400) - 1", true)] + [InlineData("1000! * 2 < 1001!", true)] + [InlineData("10^(590) >= 10^(590) + 10^(440)", false)] + [InlineData("10^(-1694) < 10^(-1694) * (1 + 10^(-150))", true)] + [InlineData("pi > 3", true)] + [InlineData("sqrt(2) < 3/2", true)] + [InlineData("exp(-300) > exp(-400)", true)] + [InlineData("10^(-20) = 2 * 10^(-20)", false)] + public void TheComparisonIsDecided(string comparison, bool expected) + => Assert.Equal(Boolean.Create(expected), comparison.ToEntity().Evaled); + + /// Where the decimals cannot tell the sides apart, or made a side 0 or infinite, nothing is answered. + [Theory] + [InlineData("exp(-1000.5) > exp(-3002.5)")] + [InlineData("exp(6000) >= exp(6100)")] + [InlineData("exp(-2000) = exp(-2000.5)")] + [InlineData("sqrt(2) < sqrt(2) + 10^(-150)")] + [InlineData("sqrt(5)^1000 = 5^500")] + public void AnUndecidedComparisonStaysWritten(string comparison) + => Assert.IsNotType(comparison.ToEntity().Evaled); + } +}