Skip to content

Commit d9a5865

Browse files
Rafael-SOWNetclaude
andcommitted
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 the four inequality nodes decided a comparison only between numbers of the real type, so cos(3) >= 0 was NaN. A number with no imaginary part is now compared as the real it is. A comparison with a value off the real line is still NaN. This also lets a condition an answer states on a sign, provided cos(x) >= 0, be evaluated at a point with the downcasting off. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent fd8fc71 commit d9a5865

4 files changed

Lines changed: 87 additions & 4 deletions

File tree

‎BREAKING-CHANGES.md‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1736,6 +1736,20 @@ where it was `0.1 * x ^ 2 / 2 + C`
17361736
| `limit(sin(c*x)/x, x, 0)` | `NaN` | `c` |
17371737
| `limit((1 - cos(c*x))/x^2, x, 0)` | `NaN` | `c ^ 2 / 2` |
17381738

1739+
### With the downcasting off, a comparison of real values is decided
1740+
1741+
With `DowncastingEnabled` off, `cos(3)` evaluates to a complex number whose imaginary part is zero,
1742+
and a comparison of one was `NaN`: inequalities were decided only between numbers of the real
1743+
type. A number with no imaginary part is now compared as the real it is, so `cos(3) >= 0` is
1744+
`false` under either setting, and a condition an answer states on a sign, `provided cos(x) >= 0`,
1745+
can be evaluated at a point. A comparison with a value off the real line is still `NaN`.
1746+
1747+
| Evaluated, with the downcasting off | Was (2.5.0) | Now |
1748+
|---|---|---|
1749+
| `cos(3) >= 0` | `NaN` | `false` |
1750+
| `sin(4) < 0` | `NaN` | `true` |
1751+
| `cos(1) > cos(3)` | `NaN` | `true` |
1752+
17391753
### A half-odd power of a logarithm beside a power of `x` is integrated
17401754

17411755
`x sqrt(ln(x))` was left unintegrated. `x^p F^n`, where `F` is `ln x` or `A + B ln(c x^r)` and

‎Sources/.editorconfig‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -666,3 +666,6 @@ file_header_template=\nCopyright (c) 2019-2026 Angouri.\nAngouriMath is licensed
666666

667667
[Tests/UnitTests/Convenience/TrigonometricIntegralTest.cs]
668668
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
669+
670+
[Tests/UnitTests/Core/DowncastingOffComparisonTest.cs]
671+
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

‎Sources/AngouriMath/Functions/Evaluation/Evaluation.Discrete/Evaluation.Discrete.Classes.cs‎

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,19 @@ protected override Entity InnerSimplify(bool isExact)
281281
propagateSet: !(Left.InnerSimplified(isExact) is Set && Right.InnerSimplified(isExact) is Set));
282282
}
283283

284+
/// <summary>
285+
/// <paramref name="number"/> as the real it is, or <see langword="null"/> where it has an
286+
/// imaginary part. With downcasting off a value such as <c>cos(3)</c> evaluates to a
287+
/// <see cref="Number.Complex"/> whose imaginary part is zero, and comparing it is as defined
288+
/// as comparing the real it equals: <c>cos(3) &gt;= 0</c> is false, not <c>NaN</c>.
289+
/// </summary>
290+
private static Number.Real? AsAReal(Number number) => number switch
291+
{
292+
Number.Real real => real,
293+
Number.Complex complex when complex.ImaginaryPart.EDecimal.IsZero => complex.RealPart,
294+
_ => null,
295+
};
296+
284297
partial record Greaterf
285298
{
286299
// Inequality comparisons are only defined for real numbers.
@@ -292,7 +305,7 @@ protected override Entity InnerSimplify(bool isExact)
292305
=> ExpandOnTwoArguments(Left, Right,
293306
(a, b) => (a, b) switch
294307
{
295-
(Real reLeft, Real reRight) => reLeft > reRight,
308+
(Number numLeft, Number numRight) when AsAReal(numLeft) is { } reLeft && AsAReal(numRight) is { } reRight => reLeft > reRight,
296309
(Number numLeft, Number numRight) => MathS.NaN,
297310
_ => null
298311
},
@@ -311,7 +324,7 @@ protected override Entity InnerSimplify(bool isExact)
311324
=> ExpandOnTwoArguments(Left, Right,
312325
(a, b) => (a, b) switch
313326
{
314-
(Real reLeft, Real reRight) => reLeft >= reRight,
327+
(Number numLeft, Number numRight) when AsAReal(numLeft) is { } reLeft && AsAReal(numRight) is { } reRight => reLeft >= reRight,
315328
(Number numLeft, Number numRight) => MathS.NaN,
316329
_ => null
317330
},
@@ -330,7 +343,7 @@ protected override Entity InnerSimplify(bool isExact)
330343
=> ExpandOnTwoArguments(Left, Right,
331344
(a, b) => (a, b) switch
332345
{
333-
(Real reLeft, Real reRight) => reLeft < reRight,
346+
(Number numLeft, Number numRight) when AsAReal(numLeft) is { } reLeft && AsAReal(numRight) is { } reRight => reLeft < reRight,
334347
(Number numLeft, Number numRight) => MathS.NaN,
335348
_ => null
336349
},
@@ -348,7 +361,7 @@ protected override Entity InnerSimplify(bool isExact)
348361
=> ExpandOnTwoArguments(Left, Right,
349362
(a, b) => (a, b) switch
350363
{
351-
(Real reLeft, Real reRight) => reLeft <= reRight,
364+
(Number numLeft, Number numRight) when AsAReal(numLeft) is { } reLeft && AsAReal(numRight) is { } reRight => reLeft <= reRight,
352365
(Number numLeft, Number numRight) => MathS.NaN,
353366
_ => null
354367
},
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
//
2+
// Copyright (c) 2019-2026 Angouri.
3+
// AngouriMath is licensed under MIT.
4+
// Details: https://github.com/asc-community/AngouriMath/blob/master/LICENSE.md.
5+
// Website: https://am.angouri.org.
6+
//
7+
8+
using AngouriMath;
9+
using Xunit;
10+
using static AngouriMath.Entity;
11+
12+
namespace AngouriMath.Tests.Core
13+
{
14+
/// <summary>
15+
/// A comparison of two numbers with no imaginary part is decided whatever the downcasting
16+
/// setting. With it off, <c>cos(3)</c> evaluates to a complex number whose imaginary part is
17+
/// zero, and a comparison of it was <c>NaN</c>, which also made every condition an answer
18+
/// stated on a sign undecidable.
19+
/// </summary>
20+
[Trait("Area", "Core")]
21+
public sealed class DowncastingOffComparisonTest
22+
{
23+
[Theory]
24+
[InlineData("cos(3) >= 0", false)]
25+
[InlineData("cos(1) >= 0", true)]
26+
[InlineData("cos(1) > cos(3)", true)]
27+
[InlineData("sin(4) < 0", true)]
28+
[InlineData("sin(4) <= -1", false)]
29+
[InlineData("cos(3) < 1 and cos(1) > 0", true)]
30+
public void AComparisonOfRealValuesIsDecided(string comparison, bool expected)
31+
{
32+
foreach (var downcasting in new[] { true, false })
33+
{
34+
using var _ = MathS.Settings.DowncastingEnabled.Set(downcasting);
35+
Assert.Equal(expected ? Boolean.True : Boolean.False, MathS.FromString(comparison, useCache: false).Evaled);
36+
}
37+
}
38+
39+
/// <summary>A comparison with a value off the real line is still not decided.</summary>
40+
[Theory]
41+
[InlineData("i >= 0")]
42+
[InlineData("(1 + i) > 1")]
43+
[InlineData("sqrt(-2) >= 0")]
44+
public void AComparisonOffTheRealLineIsNaN(string comparison)
45+
{
46+
foreach (var downcasting in new[] { true, false })
47+
{
48+
using var _ = MathS.Settings.DowncastingEnabled.Set(downcasting);
49+
Assert.Equal(MathS.NaN, MathS.FromString(comparison, useCache: false).Evaled);
50+
}
51+
}
52+
}
53+
}

0 commit comments

Comments
 (0)