-
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
155 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
|
||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters