diff --git a/site/doc/api/math.generated.md b/site/doc/api/math.generated.md index 095f935..c8e1f04 100644 --- a/site/doc/api/math.generated.md +++ b/site/doc/api/math.generated.md @@ -626,7 +626,10 @@ A complex number. out = 1 + 2i >>> i ^ 3 # i ^ 3 -out = i +out = -i +>>> -5 + (-3i) +# -5 + (-3 * i) +out = -5 - 3i ``` ## imag diff --git a/src/Kalk.Core/KalkEngine.generated.cs b/src/Kalk.Core/KalkEngine.generated.cs index 29198e3..6fcd986 100644 --- a/src/Kalk.Core/KalkEngine.generated.cs +++ b/src/Kalk.Core/KalkEngine.generated.cs @@ -1,7 +1,7 @@ //------------------------------------------------------------------------------ // // This code was generated by a tool. -// Date: 25 Mar 2024 +// Date: 26 Mar 2024 // // Changes to this file may cause incorrect behavior and will be lost if // the code is regenerated. @@ -26955,7 +26955,10 @@ private void RegisterDocumentationAuto() out = 1 + 2i >>> i ^ 3 # i ^ 3 - out = i + out = -i + >>> -5 + (-3i) + # -5 + (-3 * i) + out = -5 - 3i "; } { diff --git a/src/Kalk.Core/Model/KalkComplex.cs b/src/Kalk.Core/Model/KalkComplex.cs index 6de699a..007b6d0 100644 --- a/src/Kalk.Core/Model/KalkComplex.cs +++ b/src/Kalk.Core/Model/KalkComplex.cs @@ -12,7 +12,7 @@ namespace Kalk.Core { public struct KalkComplex : IScriptCustomType, IKalkSpannable { - private Complex _value; + private readonly Complex _value; private const double MaxRoundToZero = 1e-14; public KalkComplex(double real, double im) @@ -145,20 +145,35 @@ public override string ToString() var re = Math.Round(Re, 10, MidpointRounding.AwayFromZero); var im = Math.Round(Im, 10, MidpointRounding.AwayFromZero); - if (Math.Abs(re) > 1e-14) builder.AppendFormat(CultureInfo.CurrentCulture, "{0}", re); + if (Math.Abs(re) > MaxRoundToZero) builder.AppendFormat(CultureInfo.CurrentCulture, "{0}", re); if (HasIm) { - if (builder.Length > 0) builder.Append(" + "); - - if (Math.Abs(Math.Abs(im) - 1.0) < MaxRoundToZero) + if (builder.Length > 0) { - builder.Append("i"); + builder.Append(im < 0 ? " - " : " + "); + + if (Math.Abs(Math.Abs(im) - 1.0) < MaxRoundToZero) + { + builder.Append("i"); + } + else + { + builder.AppendFormat(CultureInfo.CurrentCulture, "{0}i", Math.Abs(im)); + } } else { - builder.AppendFormat(CultureInfo.CurrentCulture, "{0}i", im); + if (Math.Abs(Math.Abs(im) - 1.0) < MaxRoundToZero) + { + builder.Append( im < 0 ? "-i" : "i"); + } + else + { + builder.AppendFormat(CultureInfo.CurrentCulture, "{0}i", im); + } } + } if (builder.Length == 0) builder.Append("0"); diff --git a/src/Kalk.Core/Modules/MathModule.cs b/src/Kalk.Core/Modules/MathModule.cs index e3cc2e1..3270e56 100644 --- a/src/Kalk.Core/Modules/MathModule.cs +++ b/src/Kalk.Core/Modules/MathModule.cs @@ -147,7 +147,10 @@ public MathModule() /// out = 1 + 2i /// >>> i ^ 3 /// # i ^ 3 - /// out = i + /// out = -i + /// >>> -5 + (-3i) + /// # -5 + (-3 * i) + /// out = -5 - 3i /// ``` /// [KalkExport("i", CategoryMathFunctions)] diff --git a/src/Kalk.Tests/KalkTests.generated.cs b/src/Kalk.Tests/KalkTests.generated.cs index 10384ef..d2398b6 100644 --- a/src/Kalk.Tests/KalkTests.generated.cs +++ b/src/Kalk.Tests/KalkTests.generated.cs @@ -1,7 +1,7 @@ //------------------------------------------------------------------------------ // // This code was generated by a tool. -// Date: 25 Mar 2024 +// Date: 26 Mar 2024 // // Changes to this file may cause incorrect behavior and will be lost if // the code is regenerated. @@ -905,10 +905,13 @@ public partial class MathModuleTests : KalkTestBase /// Test for or `i`. /// [TestCase(@"1 + 2i -i ^ 3", @"# 1 + 2 * i +i ^ 3 +-5 + (-3i)", @"# 1 + 2 * i out = 1 + 2i # i ^ 3 -out = i", Category = "Math Functions")] +out = -i +# -5 + (-3 * i) +out = -5 - 3i", Category = "Math Functions")] public static void Test_i(string input, string output) => AssertScript(input, output); ///