Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Interpreter.BuildFormula does not work with constants #71

Open
FabianNitsche opened this issue Feb 2, 2021 · 0 comments
Open

Interpreter.BuildFormula does not work with constants #71

FabianNitsche opened this issue Feb 2, 2021 · 0 comments

Comments

@FabianNitsche
Copy link

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant