Hef.Math.Interpreter 1.1.1

.NET Framework
Install-Package Hef.Math.Interpreter -Version 1.1.1
dotnet add package Hef.Math.Interpreter --version 1.1.1
<PackageReference Include="Hef.Math.Interpreter" Version="1.1.1" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Hef.Math.Interpreter --version 1.1.1
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: Hef.Math.Interpreter, 1.1.1"
#r directive can be used in F# Interactive, C# scripting and .NET Interactive. Copy this into the interactive tool or source code of the script to reference the package.
// Install Hef.Math.Interpreter as a Cake Addin
#addin nuget:?package=Hef.Math.Interpreter&version=1.1.1

// Install Hef.Math.Interpreter as a Cake Tool
#tool nuget:?package=Hef.Math.Interpreter&version=1.1.1
The NuGet Team does not provide support for this client. Please contact its maintainers for support.


This software is based on the .Net 2.0 Framework and has no other dependencies.


The interpreter accepts two notation styles. Operations can be written as functions with parenthesis and arguments, or like regular operations with a symbol. There is actually no difference between functions and symbols in the implementation. For instance, the addition can be written add(1, 2) or 1 + 2. Or even add 1 2 or +(1, 2) if you like it.

The complete list of handled operations is availablable at Annex - Handled Operations.

Here is a simple example.

Interpreter interpreter = new Interpreter();
double result = interpreter.Calculate("sqrt(4) + 2"); // -> 4

The following example highlights the use of manually registered local and global variables.

Interpreter interpreter = new Interpreter();
Interpreter.SetGlobalVar("foo", 1d);
interpreter.SetVar("bar", 2d);
double result = interpreter.Calculate("($foo + $bar) * 2"); // -> 6

The following example highlights the use of Hef.Math.IInterpreterContext, that allows the interpreter to access variables provided by other objects.

Interpreter interpreter = new Interpreter();
interpreter.SetContext("player", new Player()));
double result = interpreter.Calculate("$player.level - 1"); // -> 9

class Player : Hef.Math.IInterpreterContext
    private int level = 10;

    public bool TryGetVariable(string name, out double value)
        value = 0d;

        if (name == "level")
            value = this.level;
            return true;

        return false;

Note About Caching

Each time a formula is calculated, the interpreter has to breaks the formula into nodes and build a tree of operations. This is a time-consumming process.

In order to make it faster, each time a new formula is processed, the intrepreder will keep the generated tree in memory (up to 64). So if the same formula is used again, the tree will be reused, and only the mathematical operations will be recomputed.

If for some reason the cache has to be manually cleared, the Interpreter provides a function to do so.

Product Versions
.NET Framework net
Compatible target framework(s)
Additional computed target framework(s)
Learn more about Target Frameworks and .NET Standard.

This package has no dependencies.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.1.1 7,348 11/2/2017
1.1.0 771 9/11/2017
1.0.0 905 9/3/2017
0.2.0-alpha 784 8/29/2017
0.1.1-alpha 646 8/22/2017
0.1.0-alpha 773 8/20/2017

- An interpreter context can now be replaced by another one with the same name.