Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions BREAKING-CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,8 @@ private static Entity MatricesAreEqual(Matrix left, Matrix right, bool isExact)
/// <inheritdoc/>
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.
Expand All @@ -341,6 +342,95 @@ protected override Entity InnerSimplify(bool isExact)
propagateSet: !(Left.InnerSimplified(isExact) is Set && Right.InnerSimplified(isExact) is Set));
}

/// <summary>
/// The sign of <paramref name="left"/> less <paramref name="right"/>, the values of
/// <paramref name="leftWritten"/> and <paramref name="rightWritten"/>, where it is decided; or
/// <see langword="null"/>.
/// </summary>
/// <remarks>
/// Two rationals are ordered exactly. Through their hundred-digit decimals, <c>10^(-200) &lt; 2 * 10^(-200)</c>
/// was <c>False</c>, both being below the decimals' least exponent, and so were <c>-10^(400) &gt; -10^(400) - 1</c> and
/// <c>1000! * 2 &lt; 1001!</c>, 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: <c>sqrt(2) &lt; sqrt(2) + 10^(-150)</c> was <c>False</c>. 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: <c>exp(-1000.5) &gt; exp(-3002.5)</c> was <c>False</c> and
/// <c>exp(6000) &gt;= exp(6100)</c> <c>True</c>. 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
/// </remarks>
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;
}

/// <summary>
/// Whether an equality of two evaluated numbers is not decided by them: a side flushed or saturated (see
/// <see cref="Ordered"/>), or two decimals that differ by no more than the downcasting tolerance relative to the
/// larger, which is rounding. <c>exp(-2000) = exp(-2000.5)</c> was <c>True</c>, both flushed to 0, and
/// <c>sqrt(5)^1000 = 5^500</c> <c>False</c>, 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
/// </summary>
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;
}

/// <summary>
/// Whether <paramref name="value"/>, 0 or an infinity, is what the decimals made of <paramref name="written"/>, a
/// product, a quotient or a power of nonzero finite constants, which is neither.
/// </summary>
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,
};
}

/// <summary>
/// <paramref name="number"/> as the real it is, or <see langword="null"/> where it has an
/// imaginary part. With downcasting off a value such as <c>cos(3)</c> evaluates to a
Expand Down Expand Up @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand Down
Original file line number Diff line number Diff line change
@@ -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
{
/// <summary>
/// A comparison of two evaluated numbers went through their hundred-digit decimals: <c>10^(-200) &lt; 2 * 10^(-200)</c>
/// was <c>False</c>, both sides below the decimals' least exponent, and <c>1000! * 2 &lt; 1001!</c> was <c>False</c>,
/// 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
/// </summary>
[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);

/// <summary>Where the decimals cannot tell the sides apart, or made a side 0 or infinite, nothing is answered.</summary>
[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<Boolean>(comparison.ToEntity().Evaled);
}
}
Loading