Skip to content

Commit 429ace7

Browse files
A complex number prints the number it holds, whatever the downcasting setting at printing (#1611)
Printing a complex number with a negative imaginary part negated that part, to print a minus sign before its magnitude. Negating makes a new number, and a new number is downcast under the setting in force when it is made. A number made with downcasting off, and printed once it was back on, printed as another number: 1 - 0.333...i, the decimal to forty digits, as 1 - 1/3i, a fraction without the product sign it needs, which reads back as 1 + i/3; and the imaginary part of erfc(16 - 0.5i), -9.49e-114, as 0. Latexize did the same. The library returns such numbers itself as well: Number.Factorial evaluates a complex argument with the downcasting off and returns what it made there. Both printers negate with the downcasting off, so the negation is the same number with its sign changed. Under one setting throughout nothing prints differently: Real.Create rounds nothing in either mode, and what it snaps to an integer or a small rational it snaps the same way at either sign. StringizeRoundTripTest gains three numbers made with downcasting off. Each reads back as itself from what Stringize prints, and Latexize prints its imaginary part's magnitude. All three fail without the change. Closes #1610. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 2eeb163 commit 429ace7

3 files changed

Lines changed: 45 additions & 2 deletions

File tree

‎Sources/AngouriMath/Functions/Output/Latex/Latex.Number.Classes.cs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ static string RenderNum(Real number)
3131
return RealPart.Latexize();
3232
else if (RealPart is Integer(0))
3333
return RenderNum(ImaginaryPart) + @"\mathrm{i}"; // Display i upright per ISO 80000-2.
34-
var (im, sign) = ImaginaryPart > 0 ? (ImaginaryPart, "+") : (-ImaginaryPart, "-");
34+
var (im, sign) = ImaginaryPart > 0 ? (ImaginaryPart, "+") : (NegatedAsItIs(ImaginaryPart), "-");
3535
return RealPart.Latexize() + " " + sign + " " +
3636
(im == 1 ? "" : im.Latexize(ImaginaryPart is Rational and not Integer)) + @"\mathrm{i}";
3737
}

‎Sources/AngouriMath/Functions/Output/ToString/ToString.Number.Classes.cs‎

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,23 @@ static string RenderNum(Real number)
4242
return RealPart.Stringize();
4343
else if (RealPart is Integer(0))
4444
return RenderNum(ImaginaryPart) + times + "i";
45-
var (im, sign) = ImaginaryPart > 0 ? (ImaginaryPart, "+") : (-ImaginaryPart, "-");
45+
var (im, sign) = ImaginaryPart > 0 ? (ImaginaryPart, "+") : (NegatedAsItIs(ImaginaryPart), "-");
4646
return RealPart.Stringize() + " " + sign + " " + RenderNum(im) + times + "i";
4747
}
48+
49+
/// <summary>
50+
/// <paramref name="number"/> negated, and nothing else. Negating makes a new number,
51+
/// which is downcast under the setting in force when it is made, and a number made
52+
/// with downcasting off could come back different: <c>0.333...</c> to forty digits
53+
/// as the rational 1/3, printed without the product sign a fraction needs, so that
54+
/// <c>1 - 0.333...i</c> read back as <c>1 + i/3</c>; and <c>9.5e-114</c> as 0.
55+
/// https://github.com/asc-community/AngouriMath/issues/1610
56+
/// </summary>
57+
internal static Real NegatedAsItIs(Real number)
58+
{
59+
using var _ = MathS.Settings.DowncastingEnabled.Set(false);
60+
return -number;
61+
}
4862
/// <inheritdoc/>
4963
public override string ToString() => Stringize();
5064
}

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

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -387,5 +387,34 @@ public void AWordContainingTheTokenIsStillAVariable(string source) =>
387387
[InlineData("integral(domain(x, ZZ), x)")]
388388
[InlineData("{ domain(x, ZZ) : x > 0 }")]
389389
public void ANarrowedCodomainRoundTrips(string source) => AssertRoundTrip(source);
390+
391+
/// <summary>
392+
/// A number prints as it is, whatever the downcasting setting when it is printed. A
393+
/// negative imaginary part was negated to be printed, which made a new number under the
394+
/// setting in force, so a number made with downcasting off printed as another one once it
395+
/// was back on: <c>1 - 0.333...i</c> as <c>1 - 1/3i</c>, which reads back as <c>1 + i/3</c>,
396+
/// and the imaginary part of <c>erfc(16 - 0.5i)</c> as 0.
397+
/// <a href="https://github.com/asc-community/AngouriMath/issues/1610">#1610</a>
398+
/// </summary>
399+
[Theory]
400+
[InlineData("1", "-0.3333333333333333333333333333333333333333")]
401+
[InlineData("2", "-0.5000000000000000000000000000001")]
402+
[InlineData("-2.8337075375928474951E-113", "-9.4899608506981397569E-114")]
403+
public void ANumberMadeWithoutDowncastingPrintsAsItIs(string real, string imaginary)
404+
{
405+
Entity.Number.Complex number;
406+
using (MathS.Settings.DowncastingEnabled.Set(false))
407+
number = Entity.Number.Complex.Create(PeterO.Numbers.EDecimal.FromString(real), PeterO.Numbers.EDecimal.FromString(imaginary));
408+
var printed = number.Stringize();
409+
var latex = number.Latexize();
410+
using (MathS.Settings.DowncastingEnabled.Set(false))
411+
{
412+
var read = MathS.FromString(printed, useCache: false).EvalNumerical();
413+
Assert.True(read.RealPart.EDecimal.CompareTo(number.RealPart.EDecimal) == 0
414+
&& read.ImaginaryPart.EDecimal.CompareTo(number.ImaginaryPart.EDecimal) == 0,
415+
$"{printed} reads back as {read}");
416+
}
417+
Assert.Contains(PeterO.Numbers.EDecimal.FromString(imaginary).Negate() + @"\mathrm{i}", latex);
418+
}
390419
}
391420
}

0 commit comments

Comments
 (0)