Running the following throws and exception, that the variable "pi" was not found:
CalculationEngine engine = new CalculationEngine(CultureInfo.InvariantCulture, ExecutionMode.Interpreted);
Func<Dictionary<string, double>, double> formula = engine.Build("pi");
Dictionary<string, double> variables = new Dictionary<string, double>();
double result = formula(variables);
However, when running this with ExecutionMode.Compiled it works as expected.
The difference is, that Interpreter that is used in ExecutionMode.Interpreted does not check for constants as DynamicCompiler does that is used in ExecutionMode.Compiled:
private static class PrecompiledMethods
{
public static double GetVariableValueOrThrow(string variableName, FormulaContext context)
{
if (context.Variables.TryGetValue(variableName, out double result))
return result;
else if (context.ConstantRegistry.IsConstantName(variableName))
return context.ConstantRegistry.GetConstantInfo(variableName).Value;
else
throw new VariableNotDefinedException($"The variable \"{variableName}\" used is not defined.");
}
}
I will create a pull request where this check of the ConstantRegistry is added to the Interpreter.
Running the following throws and exception, that the variable "pi" was not found:
However, when running this with
ExecutionMode.Compiledit works as expected.The difference is, that
Interpreterthat is used inExecutionMode.Interpreteddoes not check for constants asDynamicCompilerdoes that is used inExecutionMode.Compiled:I will create a pull request where this check of the ConstantRegistry is added to the
Interpreter.