uCalc Math Parser & Expression Evaluator for C++
Embed Dynamic Formula Evaluation in .NET Applications
If you're looking for a high-performance C++ math parser, uCalc is a robust native C++ parsing engine you can easily embed in your applications. Beyond standard math evaluators, uCalc enables developers to define custom Domain-Specific Languages (DSLs) at runtime and safely transform structured text.
A minimal example defining a function inline to calculate the area of a rectangle.
ID: 296
#include
#include "uCalc.h"
using namespace std;
using namespace uCalcSoftware;
int main() {
uCalc uc;
uc.DefineVariable("x = 5");
uc.DefineFunction("Area(length, width) = length * width");
cout << uc.Eval("Area(4, x) + 7") << endl;
}
27 #include <iostream> #include "uCalc.h" using namespace std; using namespace uCalcSoftware; int main() { uCalc uc; uc.DefineVariable("x = 5"); uc.DefineFunction("Area(length, width) = length * width"); cout << uc.Eval("Area(4, x) + 7") << endl; }
using uCalcSoftware;
var uc = new uCalc();
uc.DefineVariable("x = 5");
uc.DefineFunction("Area(length, width) = length * width");
Console.WriteLine(uc.Eval("Area(4, x) + 7"));
27 using uCalcSoftware; var uc = new uCalc(); uc.DefineVariable("x = 5"); uc.DefineFunction("Area(length, width) = length * width"); Console.WriteLine(uc.Eval("Area(4, x) + 7"));
Imports System
Imports uCalcSoftware
Public Module Program
Public Sub Main()
Dim uc As New uCalc()
uc.DefineVariable("x = 5")
uc.DefineFunction("Area(length, width) = length * width")
Console.WriteLine(uc.Eval("Area(4, x) + 7"))
End Sub
End Module
27 Imports System Imports uCalcSoftware Public Module Program Public Sub Main() Dim uc As New uCalc() uc.DefineVariable("x = 5") uc.DefineFunction("Area(length, width) = length * width") Console.WriteLine(uc.Eval("Area(4, x) + 7")) End Sub End Module
Why Choose uCalc as Your C++ Math Parser?
Here's How uCalc Compares to the Competition
While libraries like ExprTk and muParser offer robust math evaluation, their grammar is largely fixed. Conversely, parser generators like Boost.Spirit or Flex/Bison offer unlimited flexibility but require complex grammar files, code generation, and recompilation for every syntax change.
uCalc provides a fully dynamic language workbench that integrates directly into C++ applications via a clean, programmatic APIβrequiring no build steps or external dependencies.
The Feature Matrix
| Feature | uCalc SDK | ExprTk / muParser | Boost.Spirit | Flex / Bison |
| Runtime Syntax Extensibility | β Dynamic (Define at runtime) | π‘ Limited (Operators mostly fixed) | β Static (Compile-time) | β Static (Compile-time) |
| Direct Memory Binding | β Yes (Zero-copy pointers) | β Supported | β Supported | π‘ Complex |
Lazy Evaluation (ByExpr) | β Full (Short-circuiting & loops) | β Eager evaluation only | β N/A | β N/A |
| Integrated Text Transformation | β Token-Aware Transformer Β | β Math-only | π‘ Requires complex AST walks | π‘ Requires custom logic |
| Build Step Required | β None (API-driven) | β None | π΄ Heavy template metaprogramming | π΄ Requires external toolchain |
Where uCalc Shines vs. Competitors
1. Dynamic Grammar Without Code Generation (vs. Boost.Spirit)
Parser generators are incredibly powerful, but their grammars are static. To add a new operator or literal format, developers must modify a grammar file and recompile the entire application. uCalc is fully dynamic. Using the native C++ API, host applications can define new tokens, functions, and custom operators at runtime. This enables the creation of user-configurable syntax and domain-specific languages on the fly without any code generation steps.
2. High-Performance Memory Binding
For ultimate performance in C++ applications, copying data between the host application and a scripting engine creates unnecessary overhead. uCalc supports direct memory binding. When defining a variable, host applications can pass the memory address of a native C++ variable directly to the engine. The uCalc variable becomes a direct window into the host's memory, allowing changes to reflect automatically with zero-copy data interchange.
3. Lazy Evaluation and Metaprogramming (vs. ExprTk)
C++ math parsers typically use eager evaluation, calculating every argument before a function executes. uCalc introduces advanced parameter modifiers like ByExpr and ByHandle. Callbacks receive unevaluated Expression objects or symbol metadata, allowing the C++ host code to implement custom short-circuiting logic, conditional statements, and loops that only evaluate arguments when explicitly required.
4. Token-Aware Transformation (vs. std::regex)
Standard std::regex is powerful for character-level patterns but struggles with nested structures and language context. The uCalc SDK includes a structural Transformer that operates on lexical tokens. It inherently understands code structures (parentheses, strings, user-configured comments, etc.), making it far more robust for code transformation and analysis than fragile regex patterns. Transformation rules can even execute mathematical logic inline using the {@Eval} directive.
5. Configurable Sandbox Execution
Evaluating formulas provided by end-users or loaded from configuration files requires safety. uCalc isolates formula execution within a controlled engine instance. The host C++ application explicitly governs available functions, variables, and directives, ensuring that untrusted scripts cannot escape the sandbox to execute arbitrary system commands or access unauthorized files.
6. Spreadsheet-Style Reactive Updates (vs. Manual DAGs)
In traditional C++ expression evaluation, managing interconnected formulas is a complex challenge. If Formula A depends on Variable B, updating Variable B requires the host application to manually track dependencies and explicitly trigger a re-evaluation of Formula A. With standard libraries like ExprTk or muParser, building a reactive system requires the developer to write and maintain a custom Directed Acyclic Graph (DAG) to ensure calculations happen in the correct sequence.
uCalc eliminates this overhead by handling dependency resolution natively. By utilizing the overwrite: true parameter when redefining a variable or function, the uCalc engine automatically updates the internal static dependency graph. Dependent expressions and cells are updated reactively, similar to a spreadsheet. This built-in logic saves hundreds of lines of complex C++ boilerplate and significantly accelerates the development of financial models, reactive configuration systems, and data-driven applications.
So if you need a powerful C++ math parser, then the uCalc SDK has got you covered.
Using the parse-evaluate pattern for high-performance calculations in a loop with a changing variable.
ID: 574
#include
#include "uCalc.h"
using namespace std;
using namespace uCalcSoftware;
int main() {
uCalc uc;
// Define a variable 'x' that will be updated in the loop.
auto variableX = uc.DefineVariable("x");
// Parse the expression just once before the loop begins.
auto parsedExpr = uc.Parse("x^2 * 10");
cout << "Evaluating 'x^2 * 10' for x = 1 to 5:" << endl;
for (double x = 1; x <= 5; x++) {
variableX.Value(x);
// Evaluate is very fast as the parsing work is already done.
cout << "x = " << x << ", Result = " << parsedExpr.Evaluate() << endl;
}
}
Evaluating 'x^2 * 10' for x = 1 to 5:
x = 1, Result = 10
x = 2, Result = 40
x = 3, Result = 90
x = 4, Result = 160
x = 5, Result = 250 #include <iostream> #include "uCalc.h" using namespace std; using namespace uCalcSoftware; int main() { uCalc uc; // Define a variable 'x' that will be updated in the loop. auto variableX = uc.DefineVariable("x"); // Parse the expression just once before the loop begins. auto parsedExpr = uc.Parse("x^2 * 10"); cout << "Evaluating 'x^2 * 10' for x = 1 to 5:" << endl; for (double x = 1; x <= 5; x++) { variableX.Value(x); // Evaluate is very fast as the parsing work is already done. cout << "x = " << x << ", Result = " << parsedExpr.Evaluate() << endl; } }
using uCalcSoftware;
var uc = new uCalc();
// Define a variable 'x' that will be updated in the loop.
var variableX = uc.DefineVariable("x");
// Parse the expression just once before the loop begins.
var parsedExpr = uc.Parse("x^2 * 10");
Console.WriteLine("Evaluating 'x^2 * 10' for x = 1 to 5:");
for (double x = 1; x <= 5; x++) {
variableX.Value(x);
// Evaluate is very fast as the parsing work is already done.
Console.WriteLine($"x = {x}, Result = {parsedExpr.Evaluate()}");
}
Evaluating 'x^2 * 10' for x = 1 to 5:
x = 1, Result = 10
x = 2, Result = 40
x = 3, Result = 90
x = 4, Result = 160
x = 5, Result = 250 using uCalcSoftware; var uc = new uCalc(); // Define a variable 'x' that will be updated in the loop. var variableX = uc.DefineVariable("x"); // Parse the expression just once before the loop begins. var parsedExpr = uc.Parse("x^2 * 10"); Console.WriteLine("Evaluating 'x^2 * 10' for x = 1 to 5:"); for (double x = 1; x <= 5; x++) { variableX.Value(x); // Evaluate is very fast as the parsing work is already done. Console.WriteLine($"x = {x}, Result = {parsedExpr.Evaluate()}"); }
Imports System
Imports uCalcSoftware
Public Module Program
Public Sub Main()
Dim uc As New uCalc()
'// Define a variable 'x' that will be updated in the loop.
Dim variableX = uc.DefineVariable("x")
'// Parse the expression just once before the loop begins.
Dim parsedExpr = uc.Parse("x^2 * 10")
Console.WriteLine("Evaluating 'x^2 * 10' for x = 1 to 5:")
For x As Double = 1 To 5
variableX.Value(x)
'// Evaluate is very fast as the parsing work is already done.
Console.WriteLine($"x = {x}, Result = {parsedExpr.Evaluate()}")
Next
End Sub
End Module
Evaluating 'x^2 * 10' for x = 1 to 5:
x = 1, Result = 10
x = 2, Result = 40
x = 3, Result = 90
x = 4, Result = 160
x = 5, Result = 250 Imports System Imports uCalcSoftware Public Module Program Public Sub Main() Dim uc As New uCalc() '// Define a variable 'x' that will be updated in the loop. Dim variableX = uc.DefineVariable("x") '// Parse the expression just once before the loop begins. Dim parsedExpr = uc.Parse("x^2 * 10") Console.WriteLine("Evaluating 'x^2 * 10' for x = 1 to 5:") For x As Double = 1 To 5 variableX.Value(x) '// Evaluate is very fast as the parsing work is already done. Console.WriteLine($"x = {x}, Result = {parsedExpr.Evaluate()}") Next End Sub End Module
Passing arg ByExpr (delayed lazy eval) and ByHandle
ID: 14
#include
#include "uCalc.h"
using namespace std;
using namespace uCalcSoftware;
void ucalc_call MySum(uCalcBase::Callback cb) {
auto Total = 0.0;
auto Expr = cb.ArgExpr(1);
auto Start = cb.Arg(2);
auto Finish = cb.Arg(3);
auto Variable = cb.ArgItem(4);
for (double x = Start; x <= Finish; x++) {
Variable.Value(x);
Total += Expr.Evaluate();
}
cb.Return(Total);
}
int main() {
uCalc uc;
uc.DefineVariable("x");
uc.DefineFunction("Sum(ByExpr Expr, Start, Finish, ByHandle Var)", MySum);
cout << uc.Eval("Sum(x ^ 2, 1, 10, x)") << endl;
}
385 #include <iostream> #include "uCalc.h" using namespace std; using namespace uCalcSoftware; void ucalc_call MySum(uCalcBase::Callback cb) { auto Total = 0.0; auto Expr = cb.ArgExpr(1); auto Start = cb.Arg(2); auto Finish = cb.Arg(3); auto Variable = cb.ArgItem(4); for (double x = Start; x <= Finish; x++) { Variable.Value(x); Total += Expr.Evaluate(); } cb.Return(Total); } int main() { uCalc uc; uc.DefineVariable("x"); uc.DefineFunction("Sum(ByExpr Expr, Start, Finish, ByHandle Var)", MySum); cout << uc.Eval("Sum(x ^ 2, 1, 10, x)") << endl; }
using uCalcSoftware;
var uc = new uCalc();
static void MySum(uCalc.Callback cb) {
var Total = 0.0;
var Expr = cb.ArgExpr(1);
var Start = cb.Arg(2);
var Finish = cb.Arg(3);
var Variable = cb.ArgItem(4);
for (double x = Start; x <= Finish; x++) {
Variable.Value(x);
Total += Expr.Evaluate();
}
cb.Return(Total);
}
uc.DefineVariable("x");
uc.DefineFunction("Sum(ByExpr Expr, Start, Finish, ByHandle Var)", MySum);
Console.WriteLine(uc.Eval("Sum(x ^ 2, 1, 10, x)"));
385 using uCalcSoftware; var uc = new uCalc(); static void MySum(uCalc.Callback cb) { var Total = 0.0; var Expr = cb.ArgExpr(1); var Start = cb.Arg(2); var Finish = cb.Arg(3); var Variable = cb.ArgItem(4); for (double x = Start; x <= Finish; x++) { Variable.Value(x); Total += Expr.Evaluate(); } cb.Return(Total); } uc.DefineVariable("x"); uc.DefineFunction("Sum(ByExpr Expr, Start, Finish, ByHandle Var)", MySum); Console.WriteLine(uc.Eval("Sum(x ^ 2, 1, 10, x)"));
Imports System
Imports uCalcSoftware
Public Module Program
Public Sub MySum(ByVal cb As uCalc.Callback)
Dim Total = 0.0
Dim Expr = cb.ArgExpr(1)
Dim Start = cb.Arg(2)
Dim Finish = cb.Arg(3)
Dim Variable = cb.ArgItem(4)
For x As Double = Start To Finish
Variable.Value(x)
Total += Expr.Evaluate()
Next
cb.Return(Total)
End Sub
Public Sub Main()
Dim uc As New uCalc()
uc.DefineVariable("x")
uc.DefineFunction("Sum(ByExpr Expr, Start, Finish, ByHandle Var)", AddressOf MySum)
Console.WriteLine(uc.Eval("Sum(x ^ 2, 1, 10, x)"))
End Sub
End Module
385 Imports System Imports uCalcSoftware Public Module Program Public Sub MySum(ByVal cb As uCalc.Callback) Dim Total = 0.0 Dim Expr = cb.ArgExpr(1) Dim Start = cb.Arg(2) Dim Finish = cb.Arg(3) Dim Variable = cb.ArgItem(4) For x As Double = Start To Finish Variable.Value(x) Total += Expr.Evaluate() Next cb.Return(Total) End Sub Public Sub Main() Dim uc As New uCalc() uc.DefineVariable("x") uc.DefineFunction("Sum(ByExpr Expr, Start, Finish, ByHandle Var)", AddressOf MySum) Console.WriteLine(uc.Eval("Sum(x ^ 2, 1, 10, x)")) End Sub End Module