Skip to content

Interpreter.BuildFormula does not work with constants #71

@FabianNitsche

Description

@FabianNitsche

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions