uCalc API Version: 2.1.3-preview.2 Released: 6/16/2026
Warning
uCalc API Preview Release Notice:The documentation describes the intended behavior of the API. The current preview build contains incomplete features, unoptimized performance, and is subject to breaking changes.
Evaluate
Method
Product:
Class:
Evaluates a pre-parsed expression, returning a numeric result. This is the high-performance second step in the parse-evaluate workflow.
Syntax
Parameters
Return
double
The double-precision numeric result of the evaluated expression.
Remarks
🚀 High-Performance Evaluation
The Evaluate method is the high-performance second step in uCalc's two-stage evaluation process. It executes a pre-compiled Expression object, making it exceptionally fast for repeated calculations.
The Parse-Once, Evaluate-Many Pattern
For optimal performance, especially in loops or performance-critical scenarios, you should always follow this pattern:
- Parse Once: Call uCalc.Parse() outside your loop to convert the string expression into a compiled, reusable
Expressionobject. - Evaluate Many: Call
.Evaluate()on that object inside your loop.
This avoids the expensive overhead of re-parsing the same string on every iteration.
var uc = new uCalc();// Define the variable to be used in the expression.var variableX = uc.DefineVariable("x");// 1. Parse the expression once, outside the loop.var parsedExpr = uc.Parse("x * 2");// or var parsedExr = new uCalc.Expression(uc, "x * 2");// 2. Evaluate it many times inside the loop with changing variables.for (var i = 1; i <= 5; i++) { VariableX.Value(i); Console.WriteLine(parsedExpr.Evaluate()); // Extremely fast}⚙️ Data Type Handling
This method is optimized for numeric results and always returns a double. It intelligently handles various input types:
- If the expression's result is a
double, it is returned directly. - If the result is another numeric type (e.g.,
Int32,Single,Boolean), it is automatically and safely converted to adouble. - If the expression returns a non-numeric type (like a
StringorComplexnumber), the return value will be meaningless. For these cases, use EvaluateStr() instead.
🆚 Comparative Analysis: Choosing the Right Evaluation Method
uCalc provides several ways to evaluate expressions. Choose the one that best fits your use case.
| Method | Return Type | Use Case | Performance |
|---|---|---|---|
| Expression.EvaluateDbl() | double | Best for loops. High-speed numeric calculations on a pre-parsed expression. | ⭐⭐⭐⭐⭐ |
Expression.Evaluate() | double | Best for loops. High-speed numeric calculations on a pre-parsed expression. (may convert from another numeric type to Double) | ⭐⭐⭐⭐ |
| Expression.EvaluateStr() | string | High-speed evaluation for any data type (including strings) on a pre-parsed expression. | ⭐⭐⭐ |
| uCalc.Eval() | double | Convenience. One-off numeric calculations where the expression is not reused. | ⭐⭐ |
| uCalc.EvalStr() | string | Convenience. One-off calculations for any data type, especially for displaying user output. | ⭐ |
Why uCalc?
In many environments, evaluating expressions in a loop requires sub-optimal solutions.
- Native Code (e.g.,
DataTable.Computein .NET): These solutions are often slow, lack features (like custom functions), and re-parse the expression on every call, making them unsuitable for performance-critical loops. - Interpreted Languages (e.g., Python
eval): While flexible, these also parse on each call and carry the overhead of a full scripting engine.
uCalc's Parse + Evaluate model provides the best of both worlds: the flexibility of runtime expressions combined with performance that approaches compiled code, because the expensive parsing work is done only once.
Examples
A simple demonstration of parsing an expression once and then evaluating it.
using uCalcSoftware;
var uc = new uCalc();
// 1. Parse the expression into a reusable object.
var parsedExpr = uc.Parse("100 / 4");
// 2. Evaluate the pre-parsed object.
Console.WriteLine($"Result: {parsedExpr.Evaluate()}");
Result: 25 using uCalcSoftware; var uc = new uCalc(); // 1. Parse the expression into a reusable object. var parsedExpr = uc.Parse("100 / 4"); // 2. Evaluate the pre-parsed object. Console.WriteLine($"Result: {parsedExpr.Evaluate()}");
#include
#include "uCalc.h"
using namespace std;
using namespace uCalcSoftware;
int main() {
uCalc uc;
// 1. Parse the expression into a reusable object.
auto parsedExpr = uc.Parse("100 / 4");
// 2. Evaluate the pre-parsed object.
cout << "Result: " << parsedExpr.Evaluate() << endl;
}
Result: 25 #include <iostream> #include "uCalc.h" using namespace std; using namespace uCalcSoftware; int main() { uCalc uc; // 1. Parse the expression into a reusable object. auto parsedExpr = uc.Parse("100 / 4"); // 2. Evaluate the pre-parsed object. cout << "Result: " << parsedExpr.Evaluate() << endl; }
Imports System
Imports uCalcSoftware
Public Module Program
Public Sub Main()
Dim uc As New uCalc()
'// 1. Parse the expression into a reusable object.
Dim parsedExpr = uc.Parse("100 / 4")
'// 2. Evaluate the pre-parsed object.
Console.WriteLine($"Result: {parsedExpr.Evaluate()}")
End Sub
End Module
Result: 25 Imports System Imports uCalcSoftware Public Module Program Public Sub Main() Dim uc As New uCalc() '// 1. Parse the expression into a reusable object. Dim parsedExpr = uc.Parse("100 / 4") '// 2. Evaluate the pre-parsed object. Console.WriteLine($"Result: {parsedExpr.Evaluate()}") End Sub End Module
Using the parse-evaluate pattern for high-performance calculations in a loop with a changing variable.
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()}"); }
#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; } }
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
Illustrates the core relationship between the uCalc engine, an Item (variable), and a compiled Expression.
using uCalcSoftware;
var uc = new uCalc();
var VariableX = uc.DefineVariable("x");
var Expression = "x^2 * 10"; // Replace this with your expression
Console.WriteLine("--- Efficient: Parse() once, then Evaluate() in a loop ---");
var ParsedExpr = uc.Parse(Expression);
for (double x = 1; x <= 10; x++) {
VariableX.Value(x);
Console.WriteLine(ParsedExpr.Evaluate());
}
ParsedExpr.Release();
VariableX.Release();
--- Efficient: Parse() once, then Evaluate() in a loop ---
10
40
90
160
250
360
490
640
810
1000 using uCalcSoftware; var uc = new uCalc(); var VariableX = uc.DefineVariable("x"); var Expression = "x^2 * 10"; // Replace this with your expression Console.WriteLine("--- Efficient: Parse() once, then Evaluate() in a loop ---"); var ParsedExpr = uc.Parse(Expression); for (double x = 1; x <= 10; x++) { VariableX.Value(x); Console.WriteLine(ParsedExpr.Evaluate()); } ParsedExpr.Release(); VariableX.Release();
#include
#include "uCalc.h"
using namespace std;
using namespace uCalcSoftware;
int main() {
uCalc uc;
auto VariableX = uc.DefineVariable("x");
auto Expression = "x^2 * 10"; // Replace this with your expression
cout << "--- Efficient: Parse() once, then Evaluate() in a loop ---" << endl;
auto ParsedExpr = uc.Parse(Expression);
for (double x = 1; x <= 10; x++) {
VariableX.Value(x);
cout << ParsedExpr.Evaluate() << endl;
}
ParsedExpr.Release();
VariableX.Release();
}
--- Efficient: Parse() once, then Evaluate() in a loop ---
10
40
90
160
250
360
490
640
810
1000 #include <iostream> #include "uCalc.h" using namespace std; using namespace uCalcSoftware; int main() { uCalc uc; auto VariableX = uc.DefineVariable("x"); auto Expression = "x^2 * 10"; // Replace this with your expression cout << "--- Efficient: Parse() once, then Evaluate() in a loop ---" << endl; auto ParsedExpr = uc.Parse(Expression); for (double x = 1; x <= 10; x++) { VariableX.Value(x); cout << ParsedExpr.Evaluate() << endl; } ParsedExpr.Release(); VariableX.Release(); }
Imports System
Imports uCalcSoftware
Public Module Program
Public Sub Main()
Dim uc As New uCalc()
Dim VariableX = uc.DefineVariable("x")
Dim Expression = "x^2 * 10" '// Replace this with your expression
Console.WriteLine("--- Efficient: Parse() once, then Evaluate() in a loop ---")
Dim ParsedExpr = uc.Parse(Expression)
For x As Double = 1 To 10
VariableX.Value(x)
Console.WriteLine(ParsedExpr.Evaluate())
Next
ParsedExpr.Release()
VariableX.Release()
End Sub
End Module
--- Efficient: Parse() once, then Evaluate() in a loop ---
10
40
90
160
250
360
490
640
810
1000 Imports System Imports uCalcSoftware Public Module Program Public Sub Main() Dim uc As New uCalc() Dim VariableX = uc.DefineVariable("x") Dim Expression = "x^2 * 10" '// Replace this with your expression Console.WriteLine("--- Efficient: Parse() once, then Evaluate() in a loop ---") Dim ParsedExpr = uc.Parse(Expression) For x As Double = 1 To 10 VariableX.Value(x) Console.WriteLine(ParsedExpr.Evaluate()) Next ParsedExpr.Release() VariableX.Release() End Sub End Module
Evaluate() auto-conversion
using uCalcSoftware;
var uc = new uCalc();
// The int return value type in MyExprB is converted to
// Double with .Evaluate(), but not with .EvaluateDbl()
var MyExprA = uc.Parse("3.2 + 5.2");
var MyExprB = uc.Parse("int(3.2 + 5.2)");
Console.WriteLine(MyExprA.Evaluate());
Console.WriteLine(MyExprA.EvaluateDbl() == 8.4);
Console.WriteLine(MyExprB.Evaluate());
Console.WriteLine(MyExprB.EvaluateDbl() == 8);
8.4
True
8
False using uCalcSoftware; var uc = new uCalc(); // The int return value type in MyExprB is converted to // Double with .Evaluate(), but not with .EvaluateDbl() var MyExprA = uc.Parse("3.2 + 5.2"); var MyExprB = uc.Parse("int(3.2 + 5.2)"); Console.WriteLine(MyExprA.Evaluate()); Console.WriteLine(MyExprA.EvaluateDbl() == 8.4); Console.WriteLine(MyExprB.Evaluate()); Console.WriteLine(MyExprB.EvaluateDbl() == 8);
#include
#include "uCalc.h"
using namespace std;
using namespace uCalcSoftware;
#define tf(IsTrue) ((IsTrue) ? "True" : "False")
int main() {
uCalc uc;
// The int return value type in MyExprB is converted to
// Double with .Evaluate(), but not with .EvaluateDbl()
auto MyExprA = uc.Parse("3.2 + 5.2");
auto MyExprB = uc.Parse("int(3.2 + 5.2)");
cout << MyExprA.Evaluate() << endl;
cout << tf(MyExprA.EvaluateDbl() == 8.4) << endl;
cout << MyExprB.Evaluate() << endl;
cout << tf(MyExprB.EvaluateDbl() == 8) << endl;
}
8.4
True
8
False #include <iostream> #include "uCalc.h" using namespace std; using namespace uCalcSoftware; #define tf(IsTrue) ((IsTrue) ? "True" : "False") int main() { uCalc uc; // The int return value type in MyExprB is converted to // Double with .Evaluate(), but not with .EvaluateDbl() auto MyExprA = uc.Parse("3.2 + 5.2"); auto MyExprB = uc.Parse("int(3.2 + 5.2)"); cout << MyExprA.Evaluate() << endl; cout << tf(MyExprA.EvaluateDbl() == 8.4) << endl; cout << MyExprB.Evaluate() << endl; cout << tf(MyExprB.EvaluateDbl() == 8) << endl; }
Imports System
Imports uCalcSoftware
Public Module Program
Public Sub Main()
Dim uc As New uCalc()
'// The int return value type in MyExprB is converted to
'// Double with .Evaluate(), but not with .EvaluateDbl()
Dim MyExprA = uc.Parse("3.2 + 5.2")
Dim MyExprB = uc.Parse("int(3.2 + 5.2)")
Console.WriteLine(MyExprA.Evaluate())
Console.WriteLine(MyExprA.EvaluateDbl() = 8.4)
Console.WriteLine(MyExprB.Evaluate())
Console.WriteLine(MyExprB.EvaluateDbl() = 8)
End Sub
End Module
8.4
True
8
False Imports System Imports uCalcSoftware Public Module Program Public Sub Main() Dim uc As New uCalc() '// The int return value type in MyExprB is converted to '// Double with .Evaluate(), but not with .EvaluateDbl() Dim MyExprA = uc.Parse("3.2 + 5.2") Dim MyExprB = uc.Parse("int(3.2 + 5.2)") Console.WriteLine(MyExprA.Evaluate()) Console.WriteLine(MyExprA.EvaluateDbl() = 8.4) Console.WriteLine(MyExprB.Evaluate()) Console.WriteLine(MyExprB.EvaluateDbl() = 8) End Sub End Module
Using the parse-evaluate pattern for high-performance calculations in a loop with a changing variable.
using uCalcSoftware;
var uc = new uCalc();
var variableX = uc.DefineVariable("x");
var userExpression = "x * 2 + 5";
var Total = 0.0;
var UpperBound = 1000000; // One million
// Parse the expression just once before the loop begins.
var parsedExpr = uc.Parse(userExpression );
var stopwatch = System.Diagnostics.Stopwatch.StartNew();
for (double x = 1; x <= UpperBound; x++) {
variableX.Value(x);
Total = Total + parsedExpr.Evaluate();
}
stopwatch.Stop();
//Uncomment the following line to reveal the actual speed:
//Console.WriteLine($"Elapsed Milliseconds: {stopwatch.ElapsedMilliseconds} ms");
Console.Write("Sum(1, "); Console.Write(UpperBound); Console.Write(", "); Console.Write(userExpression); Console.Write(") = "); Console.Write(Total);
Sum(1, 1000000, x * 2 + 5) = 1000006000000 using uCalcSoftware; var uc = new uCalc(); var variableX = uc.DefineVariable("x"); var userExpression = "x * 2 + 5"; var Total = 0.0; var UpperBound = 1000000; // One million // Parse the expression just once before the loop begins. var parsedExpr = uc.Parse(userExpression ); var stopwatch = System.Diagnostics.Stopwatch.StartNew(); for (double x = 1; x <= UpperBound; x++) { variableX.Value(x); Total = Total + parsedExpr.Evaluate(); } stopwatch.Stop(); //Uncomment the following line to reveal the actual speed: //Console.WriteLine($"Elapsed Milliseconds: {stopwatch.ElapsedMilliseconds} ms"); Console.Write("Sum(1, "); Console.Write(UpperBound); Console.Write(", "); Console.Write(userExpression); Console.Write(") = "); Console.Write(Total);
#include
#include "uCalc.h"
using namespace std;
using namespace uCalcSoftware;
int main() {
uCalc uc;
auto variableX = uc.DefineVariable("x");
auto userExpression = "x * 2 + 5";
auto Total = 0.0;
auto UpperBound = 1000000; // One million
// Parse the expression just once before the loop begins.
auto parsedExpr = uc.Parse(userExpression );
for (double x = 1; x <= UpperBound; x++) {
variableX.Value(x);
Total = Total + parsedExpr.Evaluate();
}
cout << "Sum(1, " << UpperBound << ", " << userExpression << ") = " << (long long)Total;
}
Sum(1, 1000000, x * 2 + 5) = 1000006000000 #include <iostream> #include "uCalc.h" using namespace std; using namespace uCalcSoftware; int main() { uCalc uc; auto variableX = uc.DefineVariable("x"); auto userExpression = "x * 2 + 5"; auto Total = 0.0; auto UpperBound = 1000000; // One million // Parse the expression just once before the loop begins. auto parsedExpr = uc.Parse(userExpression ); for (double x = 1; x <= UpperBound; x++) { variableX.Value(x); Total = Total + parsedExpr.Evaluate(); } cout << "Sum(1, " << UpperBound << ", " << userExpression << ") = " << (long long)Total; }
Imports System
Imports uCalcSoftware
Public Module Program
Public Sub Main()
Dim uc As New uCalc()
Dim variableX = uc.DefineVariable("x")
Dim userExpression = "x * 2 + 5"
Dim Total = 0.0
Dim UpperBound = 1000000 '// One million
'// Parse the expression just once before the loop begins.
Dim parsedExpr = uc.Parse(userExpression )
For x As Double = 1 To UpperBound
variableX.Value(x)
Total = Total + parsedExpr.Evaluate()
Next
Console.Write("Sum(1, ")
Console.Write(UpperBound)
Console.Write(", ")
Console.Write(userExpression)
Console.Write(") = ")
Console.Write(Total)
End Sub
End Module
Sum(1, 1000000, x * 2 + 5) = 1000006000000 Imports System Imports uCalcSoftware Public Module Program Public Sub Main() Dim uc As New uCalc() Dim variableX = uc.DefineVariable("x") Dim userExpression = "x * 2 + 5" Dim Total = 0.0 Dim UpperBound = 1000000 '// One million '// Parse the expression just once before the loop begins. Dim parsedExpr = uc.Parse(userExpression ) For x As Double = 1 To UpperBound variableX.Value(x) Total = Total + parsedExpr.Evaluate() Next Console.Write("Sum(1, ") Console.Write(UpperBound) Console.Write(", ") Console.Write(userExpression) Console.Write(") = ") Console.Write(Total) End Sub End Module