Skip to content

Commit

Permalink
#150 div
Browse files Browse the repository at this point in the history
  • Loading branch information
sys27 committed Oct 20, 2016
1 parent 40b698c commit 69a8240
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 5 deletions.
64 changes: 62 additions & 2 deletions xFunc.Maths/Expressions/Div.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -54,7 +55,18 @@ public override string ToString()
/// <seealso cref="ExpressionParameters" />
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;
}

/// <summary>
Expand All @@ -67,7 +79,7 @@ public override int GetHashCode()
{
return base.GetHashCode(6091, 3457);
}

/// <summary>
/// Clones this instance.
/// </summary>
Expand All @@ -77,6 +89,54 @@ public override IExpression Clone()
return new Div(m_left.Clone(), m_right.Clone());
}

/// <summary>
/// Gets the type of the left parameter.
/// </summary>
/// <value>
/// The type of the left parameter.
/// </value>
public override ExpressionResultType LeftType
{
get
{
return ExpressionResultType.Number | ExpressionResultType.ComplexNumber;
}
}

/// <summary>
/// Gets the type of the right parameter.
/// </summary>
/// <value>
/// The type of the right parameter.
/// </value>
public override ExpressionResultType RightType
{
get
{
return ExpressionResultType.Number | ExpressionResultType.ComplexNumber;
}
}

/// <summary>
/// Gets the type of the result.
/// </summary>
/// <value>
/// The type of the result.
/// </value>
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;
}
}

}

}
34 changes: 31 additions & 3 deletions xFunc.Tests/Expressions/Maths/DivTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}

}

}

0 comments on commit 69a8240

Please sign in to comment.