Skip to content

Commit

Permalink
Complex.ToString()
Browse files Browse the repository at this point in the history
  • Loading branch information
swharden committed Jul 15, 2020
1 parent f7e5de5 commit f4c2a53
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
20 changes: 20 additions & 0 deletions src/FftSharp.Tests/ComplexTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using NUnit.Framework;
using System;
using System.Collections.Generic;
using System.Text;

namespace FftSharp.Tests
{
class ComplexTests
{
[Test]
public void Test_Complex_ToString()
{
Assert.AreEqual("0+0j", new FftSharp.Complex(0, 0).ToString());
Assert.AreEqual("3+5j", new FftSharp.Complex(3, 5).ToString());
Assert.AreEqual("-3-5j", new FftSharp.Complex(-3, -5).ToString());
Assert.AreEqual("3-5j", new FftSharp.Complex(3, -5).ToString());
Assert.AreEqual("-3+5j", new FftSharp.Complex(-3, 5).ToString());
}
}
}
8 changes: 8 additions & 0 deletions src/FftSharp/Complex.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,14 @@ public Complex(double real, double imaginary)
Imaginary = imaginary;
}

public override string ToString()
{
if (Imaginary < 0)
return $"{Real}-{-Imaginary}j";
else
return $"{Real}+{Imaginary}j";
}

public static Complex operator +(Complex a, Complex b)
{
return new Complex(a.Real + b.Real, a.Imaginary + b.Imaginary);
Expand Down

0 comments on commit f4c2a53

Please sign in to comment.