diff --git a/xFunc.Maths/Expressions/Div.cs b/xFunc.Maths/Expressions/Div.cs
index 063a4fcf4..a489c0d52 100644
--- a/xFunc.Maths/Expressions/Div.cs
+++ b/xFunc.Maths/Expressions/Div.cs
@@ -13,6 +13,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.
using System;
+using System.Numerics;
namespace xFunc.Maths.Expressions
{
@@ -54,7 +55,18 @@ public override string ToString()
///
public override object Execute(ExpressionParameters parameters)
{
- return (double)m_left.Execute(parameters) / (double)m_right.Execute(parameters);
+ var leftResult = m_left.Execute(parameters);
+ var rightResult = m_right.Execute(parameters);
+
+ if (ResultType == ExpressionResultType.ComplexNumber)
+ {
+ var leftComplex = leftResult is Complex ? (Complex)leftResult : (double)leftResult;
+ var rightComplex = rightResult is Complex ? (Complex)rightResult : (double)rightResult;
+
+ return Complex.Divide(leftComplex, rightComplex);
+ }
+
+ return (double)leftResult / (double)rightResult;
}
///
@@ -67,7 +79,7 @@ public override int GetHashCode()
{
return base.GetHashCode(6091, 3457);
}
-
+
///
/// Clones this instance.
///
@@ -77,6 +89,54 @@ public override IExpression Clone()
return new Div(m_left.Clone(), m_right.Clone());
}
+ ///
+ /// Gets the type of the left parameter.
+ ///
+ ///
+ /// The type of the left parameter.
+ ///
+ public override ExpressionResultType LeftType
+ {
+ get
+ {
+ return ExpressionResultType.Number | ExpressionResultType.ComplexNumber;
+ }
+ }
+
+ ///
+ /// Gets the type of the right parameter.
+ ///
+ ///
+ /// The type of the right parameter.
+ ///
+ public override ExpressionResultType RightType
+ {
+ get
+ {
+ return ExpressionResultType.Number | ExpressionResultType.ComplexNumber;
+ }
+ }
+
+ ///
+ /// Gets the type of the result.
+ ///
+ ///
+ /// The type of the result.
+ ///
+ public override ExpressionResultType ResultType
+ {
+ get
+ {
+ if (m_left.ResultType == ExpressionResultType.ComplexNumber || m_right.ResultType == ExpressionResultType.ComplexNumber)
+ return ExpressionResultType.ComplexNumber;
+
+ if (m_left.ResultType == ExpressionResultType.Number || m_right.ResultType == ExpressionResultType.Number)
+ return ExpressionResultType.Number;
+
+ return ExpressionResultType.Number | ExpressionResultType.ComplexNumber;
+ }
+ }
+
}
}
diff --git a/xFunc.Tests/Expressions/Maths/DivTest.cs b/xFunc.Tests/Expressions/Maths/DivTest.cs
index 6b6bf31d3..783ad5e26 100644
--- a/xFunc.Tests/Expressions/Maths/DivTest.cs
+++ b/xFunc.Tests/Expressions/Maths/DivTest.cs
@@ -13,23 +13,51 @@
// See the License for the specific language governing permissions and
// limitations under the License.
using System;
+using System.Numerics;
using xFunc.Maths.Expressions;
using Xunit;
namespace xFunc.Tests.Expressions.Maths
{
-
+
public class DivTest
{
[Fact]
- public void ExecuteTest()
+ public void ExecuteTest1()
{
- IExpression exp = new Div(new Number(1), new Number(2));
+ var exp = new Div(new Number(1), new Number(2));
Assert.Equal(1.0 / 2.0, exp.Execute());
}
+ [Fact]
+ public void ExecuteTest2()
+ {
+ var exp = new Div(new ComplexNumber(3, 2), new ComplexNumber(2, 4));
+ var expected = new Complex(0.7, -0.4);
+
+ Assert.Equal(expected, exp.Execute());
+ }
+
+ [Fact]
+ public void ExecuteTest3()
+ {
+ var exp = new Div(new Number(3), new ComplexNumber(2, 4));
+ var expected = new Complex(0.3, -0.6);
+
+ Assert.Equal(expected, exp.Execute());
+ }
+
+ [Fact]
+ public void ExecuteTest4()
+ {
+ var exp = new Div(new ComplexNumber(3, 2), new Number(2));
+ var expected = new Complex(1.5, 1);
+
+ Assert.Equal(expected, exp.Execute());
+ }
+
}
}