Skip to content

Commit

Permalink
Added support for exponentation
Browse files Browse the repository at this point in the history
  • Loading branch information
pieterderycke committed Oct 15, 2012
1 parent fcbe8f6 commit 7c38a26
Show file tree
Hide file tree
Showing 10 changed files with 79 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Jace.Benchmark/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ static void Main(string[] args)
Console.WriteLine("Number Of Variables Of Each Function: {0}", 3);
Console.WriteLine("Number Of Executions For Each Function: {0}", 10000.ToString("N0"));
Console.WriteLine("Total Number Of Executions: {0}", (10000 * 1000).ToString("N0"));
Console.WriteLine("Parallell: {0}", true);
Console.WriteLine("Parallel: {0}", true);
Console.WriteLine();

List<string> functions = GenerateRandomFunctions(1000);
Expand Down
7 changes: 7 additions & 0 deletions Jace.DemoApp/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,13 @@ private TreeViewItem CreateTreeViewItem(Operation operation)
item.Items.Add(CreateTreeViewItem(division.Dividend));
item.Items.Add(CreateTreeViewItem(division.Divisor));
}
else if (operation.GetType() == typeof(Exponentiation))
{
Exponentiation exponentiation = (Exponentiation)operation;

item.Items.Add(CreateTreeViewItem(exponentiation.Base));
item.Items.Add(CreateTreeViewItem(exponentiation.Exponent));
}

return item;
}
Expand Down
12 changes: 12 additions & 0 deletions Jace.Tests/AstBuilderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,18 @@ public void TestMultiplication()
Assert.AreEqual(new FloatingPointConstant(2.0), multiplication.Argument2);
}

[TestMethod]
public void TestExponentiation()
{
AstBuilder builder = new AstBuilder();
Operation operation = builder.Build(new List<object>() { 2, '^', 3 });

Exponentiation exponentiation = (Exponentiation)operation;

Assert.AreEqual(new IntegerConstant(2), exponentiation.Base);
Assert.AreEqual(new IntegerConstant(3), exponentiation.Exponent);
}

[TestMethod]
public void TestVariable()
{
Expand Down
18 changes: 18 additions & 0 deletions Jace.Tests/CalculationEngineTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,24 @@ public void TestCalculateFormula1()
Assert.AreEqual(5.0, result);
}

[TestMethod]
public void TestCalculatePowCompiled()
{
CalculationEngine engine = new CalculationEngine(CultureInfo.InvariantCulture, ExecutionMode.Compiled);
double result = engine.Calculate("2^3.0");

Assert.AreEqual(8.0, result);
}

[TestMethod]
public void TestCalculatePowInterpreted()
{
CalculationEngine engine = new CalculationEngine(CultureInfo.InvariantCulture, ExecutionMode.Interpreted);
double result = engine.Calculate("2^3.0");

Assert.AreEqual(8.0, result);
}

[TestMethod]
public void TestCalculateFormulaWithVariables()
{
Expand Down
6 changes: 6 additions & 0 deletions Jace/AstBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ public AstBuilder()
operationPrecedence.Add('-', 1);
operationPrecedence.Add('*', 2);
operationPrecedence.Add('/', 2);
operationPrecedence.Add('^', 3);
}

public Operation Build(IList<object> tokens)
Expand Down Expand Up @@ -122,6 +123,11 @@ private Operation Convert(char operation)
Operation divident = resultStack.Pop();

return new Division(DataType.FloatingPoint, divident, divisor);
case '^':
Operation exponent = resultStack.Pop();
Operation @base = resultStack.Pop();

return new Exponentiation(DataType.FloatingPoint, @base, exponent);
default:
throw new ArgumentException(string.Format("Unknown operation \"{0}\".", operation), "operation");
}
Expand Down
8 changes: 8 additions & 0 deletions Jace/DynamicCompiler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,14 @@ private void GenerateMethodBody(ILGenerator generator, Operation operation)

generator.Emit(OpCodes.Div);
}
else if (operation.GetType() == typeof(Exponentiation))
{
Exponentiation exponentation = (Exponentiation)operation;
GenerateMethodBody(generator, exponentation.Base);
GenerateMethodBody(generator, exponentation.Exponent);

generator.Emit(OpCodes.Call, typeof(Math).GetMethod("Pow"));
}
else
{
throw new ArgumentException(string.Format("Unsupported operation \"{0}\".", operation.GetType().FullName), "operation");
Expand Down
5 changes: 5 additions & 0 deletions Jace/Interpreter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,11 @@ public double Execute(Operation operation, Dictionary<string, double> variables)
Division division = (Division)operation;
return Execute(division.Dividend, variables) / Execute(division.Divisor, variables);
}
else if (operation.GetType() == typeof(Exponentiation))
{
Exponentiation exponentiation = (Exponentiation)operation;
return Math.Pow(Execute(exponentiation.Base, variables), Execute(exponentiation.Exponent, variables));
}
else
{
throw new ArgumentException(string.Format("Unsupported operation \"{0}\".", operation.GetType().FullName), "operation");
Expand Down
1 change: 1 addition & 0 deletions Jace/Jace.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
<Compile Include="Operations\Constant.cs" />
<Compile Include="Operations\ConversionToFloatingPoint.cs" />
<Compile Include="Operations\Division.cs" />
<Compile Include="Operations\Exponentiation.cs" />
<Compile Include="Operations\Operation.cs" />
<Compile Include="Operations\Multiplication.cs" />
<Compile Include="Operations\Substraction.cs" />
Expand Down
20 changes: 20 additions & 0 deletions Jace/Operations/Exponentiation.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Jace.Operations
{
public class Exponentiation : Operation
{
public Exponentiation(DataType dataType, Operation @base, Operation exponent)
: base(dataType)
{
Base = @base;
Exponent = exponent;
}

public Operation Base { get; private set; }
public Operation Exponent { get; private set; }
}
}
1 change: 1 addition & 0 deletions Jace/Tokenizer/TokenReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ public List<object> Read(string formula)
case '-':
case '*':
case '/':
case '^':
case '(':
case ')':
tokens.Add(characters[i]);
Expand Down

0 comments on commit 7c38a26

Please sign in to comment.