Skip to content

Commit

Permalink
Fix #44 bis
Browse files Browse the repository at this point in the history
  • Loading branch information
xoofx committed Mar 26, 2024
1 parent b38a84e commit dfb1939
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 14 deletions.
5 changes: 4 additions & 1 deletion site/doc/api/math.generated.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 5 additions & 2 deletions src/Kalk.Core/KalkEngine.generated.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//------------------------------------------------------------------------------
// <auto-generated>
// 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.
Expand Down Expand Up @@ -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
";
}
{
Expand Down
29 changes: 22 additions & 7 deletions src/Kalk.Core/Model/KalkComplex.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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");
Expand Down
5 changes: 4 additions & 1 deletion src/Kalk.Core/Modules/MathModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
/// ```
/// </example>
[KalkExport("i", CategoryMathFunctions)]
Expand Down
9 changes: 6 additions & 3 deletions src/Kalk.Tests/KalkTests.generated.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//------------------------------------------------------------------------------
// <auto-generated>
// 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.
Expand Down Expand Up @@ -905,10 +905,13 @@ public partial class MathModuleTests : KalkTestBase
/// Test for <see cref="M:Kalk.Core.MathModule.ComplexNumber"/> or `i`.
/// </summary>
[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);

/// <summary>
Expand Down

0 comments on commit dfb1939

Please sign in to comment.