diff --git a/xFunc.Maths/Expressions/Mod.cs b/xFunc.Maths/Expressions/Mod.cs new file mode 100644 index 000000000..2345c6e3d --- /dev/null +++ b/xFunc.Maths/Expressions/Mod.cs @@ -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 +{ + + /// + /// Represents the Modulo operator. + /// + /// + public class Mod : BinaryExpression + { + + internal Mod() { } + + /// + /// Initializes a new instance of the class. + /// + /// The left (first) operand. + /// The right (second) operand. + public Mod(IExpression left, IExpression right) : base(left, right) { } + + /// + /// Returns a hash code for this instance. + /// + /// + /// A hash code for this instance, suitable for use in hashing algorithms and data structures like a hash table. + /// + public override int GetHashCode() + { + return base.GetHashCode(32909, 52541); + } + + /// + /// Returns a that represents this instance. + /// + /// + /// A that represents this instance. + /// + public override string ToString() + { + if (m_parent is BinaryExpression && !(m_parent is Mul)) + return ToString("({0} % {1})"); + + return ToString("{0} % {1}"); + } + + /// + /// Executes this expression. + /// + /// An object that contains all parameters and functions for expressions. + /// + /// A result of the execution. + /// + /// + public override object Execute(ExpressionParameters parameters) + { + var leftResult = (double)m_left.Execute(parameters); + var rightResult = (double)m_right.Execute(parameters); + + return leftResult % rightResult; + } + + /// + /// Creates the clone of this instance. + /// + /// + /// Returns the new instance of that is a clone of this instance. + /// + public override IExpression Clone() + { + return new Mod(m_left.Clone(), m_right.Clone()); + } + + } + +} diff --git a/xFunc.Maths/xFunc.Maths.csproj b/xFunc.Maths/xFunc.Maths.csproj index bc799452d..9a5a73fc4 100644 --- a/xFunc.Maths/xFunc.Maths.csproj +++ b/xFunc.Maths/xFunc.Maths.csproj @@ -77,6 +77,7 @@ + diff --git a/xFunc.Tests/Expressions/Maths/ModTest.cs b/xFunc.Tests/Expressions/Maths/ModTest.cs new file mode 100644 index 000000000..38119964e --- /dev/null +++ b/xFunc.Tests/Expressions/Maths/ModTest.cs @@ -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); + } + + } + +} diff --git a/xFunc.Tests/xFunc.Tests.csproj b/xFunc.Tests/xFunc.Tests.csproj index ac7807404..fa650b1d5 100644 --- a/xFunc.Tests/xFunc.Tests.csproj +++ b/xFunc.Tests/xFunc.Tests.csproj @@ -111,6 +111,7 @@ +