diff --git a/BREAKING-CHANGES.md b/BREAKING-CHANGES.md index 88079fc99..dd5a2d106 100644 --- a/BREAKING-CHANGES.md +++ b/BREAKING-CHANGES.md @@ -1753,6 +1753,20 @@ where it was `0.1 * x ^ 2 / 2 + C` | `limit(sin(c*x)/x, x, 0)` | `NaN` | `c` | | `limit((1 - cos(c*x))/x^2, x, 0)` | `NaN` | `c ^ 2 / 2` | +### With the downcasting off, a comparison of real values is decided + +With `DowncastingEnabled` off, `cos(3)` evaluates to a complex number whose imaginary part is zero, +and a comparison of one was `NaN`: inequalities were decided only between numbers of the real +type. A number with no imaginary part is now compared as the real it is, so `cos(3) >= 0` is +`false` under either setting, and a condition an answer states on a sign, `provided cos(x) >= 0`, +can be evaluated at a point. A comparison with a value off the real line is still `NaN`. + +| Evaluated, with the downcasting off | Was (2.5.0) | Now | +|---|---|---| +| `cos(3) >= 0` | `NaN` | `false` | +| `sin(4) < 0` | `NaN` | `true` | +| `cos(1) > cos(3)` | `NaN` | `true` | + ### A half-odd power of a logarithm beside a power of `x` is integrated `x sqrt(ln(x))` was left unintegrated. `x^p F^n`, where `F` is `ln x` or `A + B ln(c x^r)` and diff --git a/Sources/.editorconfig b/Sources/.editorconfig index 238627be5..435b4248b 100644 --- a/Sources/.editorconfig +++ b/Sources/.editorconfig @@ -669,3 +669,6 @@ file_header_template=\nCopyright (c) 2019-2026 Angouri.\nAngouriMath is licensed [Tests/UnitTests/Calculus/TrigonometricIntegralIntegrationTest.cs] file_header_template=\nCopyright (c) 2019-2026 Angouri.\nAngouriMath is licensed under MIT.\nDetails: https://github.com/asc-community/AngouriMath/blob/master/LICENSE.md.\nWebsite: https://am.angouri.org.\n + +[Tests/UnitTests/Core/DowncastingOffComparisonTest.cs] +file_header_template=\nCopyright (c) 2019-2026 Angouri.\nAngouriMath is licensed under MIT.\nDetails: https://github.com/asc-community/AngouriMath/blob/master/LICENSE.md.\nWebsite: https://am.angouri.org.\n 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 13f9a7a69..542a8d0fd 100644 --- a/Sources/AngouriMath/Functions/Evaluation/Evaluation.Discrete/Evaluation.Discrete.Classes.cs +++ b/Sources/AngouriMath/Functions/Evaluation/Evaluation.Discrete/Evaluation.Discrete.Classes.cs @@ -281,6 +281,19 @@ protected override Entity InnerSimplify(bool isExact) propagateSet: !(Left.InnerSimplified(isExact) is Set && Right.InnerSimplified(isExact) is Set)); } + /// + /// as the real it is, or where it has an + /// imaginary part. With downcasting off a value such as cos(3) evaluates to a + /// whose imaginary part is zero, and comparing it is as defined + /// as comparing the real it equals: cos(3) >= 0 is false, not NaN. + /// + private static Number.Real? AsAReal(Number number) => number switch + { + Number.Real real => real, + Number.Complex complex when complex.ImaginaryPart.EDecimal.IsZero => complex.RealPart, + _ => null, + }; + partial record Greaterf { // Inequality comparisons are only defined for real numbers. @@ -292,7 +305,7 @@ protected override Entity InnerSimplify(bool isExact) => ExpandOnTwoArguments(Left, Right, (a, b) => (a, b) switch { - (Real reLeft, Real reRight) => reLeft > reRight, + (Number numLeft, Number numRight) when AsAReal(numLeft) is { } reLeft && AsAReal(numRight) is { } reRight => reLeft > reRight, (Number numLeft, Number numRight) => MathS.NaN, _ => null }, @@ -311,7 +324,7 @@ protected override Entity InnerSimplify(bool isExact) => ExpandOnTwoArguments(Left, Right, (a, b) => (a, b) switch { - (Real reLeft, Real reRight) => reLeft >= reRight, + (Number numLeft, Number numRight) when AsAReal(numLeft) is { } reLeft && AsAReal(numRight) is { } reRight => reLeft >= reRight, (Number numLeft, Number numRight) => MathS.NaN, _ => null }, @@ -330,7 +343,7 @@ protected override Entity InnerSimplify(bool isExact) => ExpandOnTwoArguments(Left, Right, (a, b) => (a, b) switch { - (Real reLeft, Real reRight) => reLeft < reRight, + (Number numLeft, Number numRight) when AsAReal(numLeft) is { } reLeft && AsAReal(numRight) is { } reRight => reLeft < reRight, (Number numLeft, Number numRight) => MathS.NaN, _ => null }, @@ -348,7 +361,7 @@ protected override Entity InnerSimplify(bool isExact) => ExpandOnTwoArguments(Left, Right, (a, b) => (a, b) switch { - (Real reLeft, Real reRight) => reLeft <= reRight, + (Number numLeft, Number numRight) when AsAReal(numLeft) is { } reLeft && AsAReal(numRight) is { } reRight => reLeft <= reRight, (Number numLeft, Number numRight) => MathS.NaN, _ => null }, diff --git a/Sources/Tests/UnitTests/Core/DowncastingOffComparisonTest.cs b/Sources/Tests/UnitTests/Core/DowncastingOffComparisonTest.cs new file mode 100644 index 000000000..095bfe9bc --- /dev/null +++ b/Sources/Tests/UnitTests/Core/DowncastingOffComparisonTest.cs @@ -0,0 +1,53 @@ +// +// 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; +using Xunit; +using static AngouriMath.Entity; + +namespace AngouriMath.Tests.Core +{ + /// + /// A comparison of two numbers with no imaginary part is decided whatever the downcasting + /// setting. With it off, cos(3) evaluates to a complex number whose imaginary part is + /// zero, and a comparison of it was NaN, which also made every condition an answer + /// stated on a sign undecidable. + /// + [Trait("Area", "Core")] + public sealed class DowncastingOffComparisonTest + { + [Theory] + [InlineData("cos(3) >= 0", false)] + [InlineData("cos(1) >= 0", true)] + [InlineData("cos(1) > cos(3)", true)] + [InlineData("sin(4) < 0", true)] + [InlineData("sin(4) <= -1", false)] + [InlineData("cos(3) < 1 and cos(1) > 0", true)] + public void AComparisonOfRealValuesIsDecided(string comparison, bool expected) + { + foreach (var downcasting in new[] { true, false }) + { + using var _ = MathS.Settings.DowncastingEnabled.Set(downcasting); + Assert.Equal(expected ? Boolean.True : Boolean.False, MathS.FromString(comparison, useCache: false).Evaled); + } + } + + /// A comparison with a value off the real line is still not decided. + [Theory] + [InlineData("i >= 0")] + [InlineData("(1 + i) > 1")] + [InlineData("sqrt(-2) >= 0")] + public void AComparisonOffTheRealLineIsNaN(string comparison) + { + foreach (var downcasting in new[] { true, false }) + { + using var _ = MathS.Settings.DowncastingEnabled.Set(downcasting); + Assert.Equal(MathS.NaN, MathS.FromString(comparison, useCache: false).Evaled); + } + } + } +}