Skip to content

Commit

Permalink
#153 added mod expression
Browse files Browse the repository at this point in the history
  • Loading branch information
sys27 committed Nov 8, 2016
1 parent 15c16cc commit 682d620
Show file tree
Hide file tree
Showing 4 changed files with 155 additions and 0 deletions.
90 changes: 90 additions & 0 deletions xFunc.Maths/Expressions/Mod.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
// Copyright 2012-2016 Dmitry Kischenko
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
// express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
using System;

namespace xFunc.Maths.Expressions
{

/// <summary>
/// Represents the Modulo operator.
/// </summary>
/// <seealso cref="xFunc.Maths.Expressions.BinaryExpression" />
public class Mod : BinaryExpression
{

internal Mod() { }

/// <summary>
/// Initializes a new instance of the <see cref="Mod"/> class.
/// </summary>
/// <param name="left">The left (first) operand.</param>
/// <param name="right">The right (second) operand.</param>
public Mod(IExpression left, IExpression right) : base(left, right) { }

/// <summary>
/// Returns a hash code for this instance.
/// </summary>
/// <returns>
/// A hash code for this instance, suitable for use in hashing algorithms and data structures like a hash table.
/// </returns>
public override int GetHashCode()
{
return base.GetHashCode(32909, 52541);
}

/// <summary>
/// Returns a <see cref="System.String" /> that represents this instance.
/// </summary>
/// <returns>
/// A <see cref="System.String" /> that represents this instance.
/// </returns>
public override string ToString()
{
if (m_parent is BinaryExpression && !(m_parent is Mul))
return ToString("({0} % {1})");

return ToString("{0} % {1}");
}

/// <summary>
/// Executes this expression.
/// </summary>
/// <param name="parameters">An object that contains all parameters and functions for expressions.</param>
/// <returns>
/// A result of the execution.
/// </returns>
/// <seealso cref="ExpressionParameters" />
public override object Execute(ExpressionParameters parameters)
{
var leftResult = (double)m_left.Execute(parameters);
var rightResult = (double)m_right.Execute(parameters);

return leftResult % rightResult;
}

/// <summary>
/// Creates the clone of this instance.
/// </summary>
/// <returns>
/// Returns the new instance of <see cref="BinaryExpression" /> that is a clone of this instance.
/// </returns>
public override IExpression Clone()
{
return new Mod(m_left.Clone(), m_right.Clone());
}

}

}
1 change: 1 addition & 0 deletions xFunc.Maths/xFunc.Maths.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@
<Compile Include="Expressions\Matrices\Transpose.cs" />
<Compile Include="Expressions\Matrices\Vector.cs" />
<Compile Include="Expressions\Del.cs" />
<Compile Include="Expressions\Mod.cs" />
<Compile Include="Expressions\ParameterTypeMismatchException.cs" />
<Compile Include="Expressions\Product.cs" />
<Compile Include="Expressions\Programming\AddAssign.cs" />
Expand Down
63 changes: 63 additions & 0 deletions xFunc.Tests/Expressions/Maths/ModTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
// Copyright 2012-2016 Dmitry Kischenko
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
// express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
using System;
using xFunc.Maths.Expressions;
using Xunit;

namespace xFunc.Tests.Expressions.Maths
{

public class ModTest
{

[Fact]
public void ExecuteTest1()
{
var exp = new Mod(new Number(25), new Number(7));
var result = exp.Execute();

Assert.Equal(4.0, result);
}

[Fact]
public void ExecuteTest2()
{
var exp = new Mod(new Number(25), new Number(5));
var result = exp.Execute();

Assert.Equal(0.0, result);
}

[Fact]
public void ExecuteTest3()
{
var exp = new Mod(new Number(0), new Number(5));
var result = exp.Execute();

Assert.Equal(0.0, result);
}

[Fact]
public void ExecuteTest4()
{
var exp = new Mod(new Number(5), new Number(0));
var result = exp.Execute();

Assert.Equal(double.NaN, result);
}

}

}
1 change: 1 addition & 0 deletions xFunc.Tests/xFunc.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@
<Compile Include="Expressions\Maths\Matrices\MatrixTest.cs" />
<Compile Include="Expressions\Maths\Matrices\TransposeTest.cs" />
<Compile Include="Expressions\Maths\Matrices\VectorTest.cs" />
<Compile Include="Expressions\Maths\ModTest.cs" />
<Compile Include="Expressions\Maths\MulTest.cs" />
<Compile Include="Expressions\Maths\PowTest.cs" />
<Compile Include="Expressions\Maths\ProductTest.cs" />
Expand Down

0 comments on commit 682d620

Please sign in to comment.