Skip to content

Commit 806f5f6

Browse files
Compiled real and imaginary parts of a value that is not there are not there (#2312)
Both compilers take Re and Im of a System.Numerics.Complex as the one part alone, so a NaN in the other part was dropped. x / x at 0 compiles to NaN + NaN i, its imaginary part to NaN with an imaginary part of 0, and the imaginary part of that to 0: Im(Im(x / x)) and Im(Re(x / x)) compiled to 0 at 0, where the interpreter, and the compiled x / x, have no value. A NaN in either part is now the whole number's, as Im of a NaN double already was. Found among work/compilesweep's findings, as Im(Im(min(x, x))) off the real line. No issue was filed. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
1 parent 3fc4022 commit 806f5f6

2 files changed

Lines changed: 36 additions & 2 deletions

File tree

‎Sources/AngouriMath/Numerics/ComplexParts.cs‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,15 @@ internal static class ComplexParts
3030
private static double Arg(double re, double im)
3131
=> re == 0 && im == 0 ? double.NaN : Math.Atan2(im == 0 ? 0.0 : im, re == 0 ? 0.0 : re);
3232

33-
public static NumericsComplex Re(NumericsComplex z) => z.Real;
33+
/// <summary>
34+
/// The real and the imaginary part, which a number with no value does not have either: a
35+
/// NaN in one part is the whole number's, as a NaN over the doubles has an imaginary part
36+
/// of NaN. Taking only the other part gave <c>Im(Im(x / x))</c> the value 0 at 0, where
37+
/// <c>x / x</c> has none.
38+
/// </summary>
39+
public static NumericsComplex Re(NumericsComplex z) => double.IsNaN(z.Imaginary) ? double.NaN : z.Real;
3440
public static double Re(double x) => x;
35-
public static NumericsComplex Im(NumericsComplex z) => z.Imaginary;
41+
public static NumericsComplex Im(NumericsComplex z) => double.IsNaN(z.Real) ? double.NaN : z.Imaginary;
3642
public static double Im(double x) => double.IsNaN(x) ? double.NaN : 0;
3743
}
3844
}

‎Sources/Tests/UnitTests/Convenience/ComplexPartsCompilationTest.cs‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,34 @@ public void TheArgumentOfZeroHasNoValue()
5959
Assert.Equal(MathS.NaN, "arg(0)".ToEntity().Evaled);
6060
}
6161

62+
/// <summary>
63+
/// The parts of a value that is not there are not there either. <c>x / x</c> at 0 compiles
64+
/// to <c>NaN + NaN i</c>, its imaginary part to <c>NaN</c>, and the imaginary part of that
65+
/// was <c>0</c>, a value where the interpreter has none.
66+
/// </summary>
67+
[Theory]
68+
[InlineData("Im(Im(x / x))")]
69+
[InlineData("Im(Re(x / x))")]
70+
[InlineData("Re(Im(x / x))")]
71+
[InlineData("Im(Im(sqrt(x) / sqrt(x)))")]
72+
public void APartOfNoValueHasNoValue(string expression)
73+
{
74+
Assert.True(expression.ToEntity().Substitute("x", 0).Evaled.IsNaN);
75+
Assert.True(double.IsNaN(expression.ToEntity().Compile("x").Call(0).Real), $"FastExpression gives {expression.ToEntity().Compile("x").Call(0)}");
76+
Assert.True(double.IsNaN(expression.ToEntity().Compile<NumericsComplex, NumericsComplex>("x")(0).Real),
77+
$"LINQ gives {expression.ToEntity().Compile<NumericsComplex, NumericsComplex>("x")(0)}");
78+
}
79+
80+
/// <summary>A NaN in either part of an argument is the whole argument's.</summary>
81+
[Theory]
82+
[InlineData("Re(x)", 1, double.NaN)]
83+
[InlineData("Im(x)", double.NaN, 1)]
84+
public void ANaNInOnePartIsTheWholeNumbers(string expression, double re, double im)
85+
{
86+
Assert.True(double.IsNaN(expression.ToEntity().Compile("x").Call(new NumericsComplex(re, im)).Real));
87+
Assert.True(double.IsNaN(expression.ToEntity().Compile<NumericsComplex, NumericsComplex>("x")(new NumericsComplex(re, im)).Real));
88+
}
89+
6290
/// <summary>Over doubles, the argument of a negative number is pi, and the imaginary part is 0.</summary>
6391
[Theory]
6492
[InlineData("arg(x)", -2, System.Math.PI)]

0 commit comments

Comments
 (0)