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.

uCalc Fast Math Parser

Product: 

Class: 

An overview of uCalc's high-performance expression evaluation engine, highlighting its features, use cases, and dynamic capabilities.

Remarks

🚀 uCalc Fast Math Parser: More Than Just a Calculator

The uCalc Fast Math Parser is a high-performance, embeddable engine that brings the power of a dynamic scripting language to your application. It allows you to parse and evaluate mathematical, logical, and string-based expressions provided as plain text at runtime. It's not just a calculator; it's a complete, sandboxed language environment that you can customize and extend on the fly.

This overview covers the core advantages that make the uCalc expression parser a superior choice for a wide range of applications, from financial modeling and scientific computing to game scripting and dynamic report generation.


1. ⚡ Blazing-Fast Performance: The Parse-Once, Evaluate-Many Pattern

The single most important architectural feature for performance is the separation of parsing and evaluation. While convenience methods like EvalStr are great for one-off calculations, true performance is achieved by parsing an expression once and evaluating it many times.

  • Parsing: The computationally expensive step of analyzing a string and building an executable plan.
  • Evaluation: The extremely fast step of executing that plan.

By calling Parse outside a loop and Evaluate inside, you can achieve execution speeds that approach native compiled code.

💡 Why uCalc? (vs. .NET DataTable.Compute)

A common workaround for evaluating strings in .NET is DataTable.Compute. This method is notoriously slow because it must re-parse the expression string on every single call, making it completely unsuitable for performance-critical loops. uCalc's two-step model provides a C#-accessible engine with C++-level performance.


2. 🔧 Dynamic at Runtime: Build Your Own Language

Unlike compiled languages where the syntax is fixed, uCalc's grammar is fully extensible at runtime. You can teach the engine new tricks without recompiling your application.

  • Custom Functions: Use DefineFunction to create new functions, either as simple inline expressions ("InToCm(x) = x * 2.54") or by binding to high-performance native code in your application.
  • Custom Operators: Use DefineOperator to invent new operators with custom precedence and associativity, allowing you to create expressive, human-readable Domain-Specific Languages (DSLs).

💡 Why uCalc? (vs. Static Languages)

In C# or C++, the language's syntax is immutable. With uCalc, you can load new function and operator definitions from configuration files, user scripts, or a database at startup, allowing your application to adapt its own syntax on the fly.


3. 🛡️ Safe and Sandboxed Execution

Many scripting languages (like Python or JavaScript) include a built-in eval() function. While powerful, these functions often pose a major security risk because they can execute arbitrary code, potentially accessing the file system or network.

💡 Why uCalc? (vs. Scripting eval())

The uCalc engine is a secure sandbox. An expression can only access the functions, operators, and variables that you have explicitly defined within its instance. It has no intrinsic ability to interact with the host operating system, which prevents code injection vulnerabilities and ensures that user-provided formulas can be evaluated safely.


4. ✨ A Rich Built-in Library

Out of the box, the parser includes a comprehensive library of over 100 built-in Functions and Operators, including:

  • Full suite of mathematical and trigonometric functions.
  • String manipulation functions (SubStr, UCase, Trim, etc.).
  • Logical and control-flow functions like IIf (a lazily-evaluated ternary operator).

Examples

A simple calculation to convert a temperature from Celsius to Fahrenheit, demonstrating basic arithmetic and order of operations.
				
					using uCalcSoftware;

var uc = new uCalc();
Console.WriteLine(uc.EvalStr("(37) * (9 / 5) + 32"));
				
			
98.6
				
					#include <iostream>
#include "uCalc.h"

using namespace std;
using namespace uCalcSoftware;

int main() {
   uCalc uc;
   cout << uc.EvalStr("(37) * (9 / 5) + 32") << endl;
}
				
			
98.6
				
					Imports System
Imports uCalcSoftware
Public Module Program
   Public Sub Main()
      Dim uc As New uCalc()
      Console.WriteLine(uc.EvalStr("(37) * (9 / 5) + 32"))
   End Sub
End Module
				
			
98.6
Calculates a monthly loan payment by defining a custom function with the standard amortization formula, showcasing a practical, real-world use case.
				
					using uCalcSoftware;

var uc = new uCalc();
// Define a function for the standard loan payment formula
uc.DefineFunction("LoanPmt(rate, nper, pv) = Int((rate * pv) / (1 - (1 + rate)^-nper)*100)/100");

// Define variables for the calculation
uc.DefineVariable("monthly_rate = 0.05 / 12"); // 5% annual rate
uc.DefineVariable("periods = 30 * 12");      // 30 years
uc.DefineVariable("loan_amount = 200000");   // $200,000

Console.WriteLine(uc.EvalStr("LoanPmt(monthly_rate, periods, loan_amount)"));
				
			
1073.64
				
					#include <iostream>
#include "uCalc.h"

using namespace std;
using namespace uCalcSoftware;

int main() {
   uCalc uc;
   // Define a function for the standard loan payment formula
   uc.DefineFunction("LoanPmt(rate, nper, pv) = Int((rate * pv) / (1 - (1 + rate)^-nper)*100)/100");

   // Define variables for the calculation
   uc.DefineVariable("monthly_rate = 0.05 / 12"); // 5% annual rate
   uc.DefineVariable("periods = 30 * 12");      // 30 years
   uc.DefineVariable("loan_amount = 200000");   // $200,000

   cout << uc.EvalStr("LoanPmt(monthly_rate, periods, loan_amount)") << endl;
}
				
			
1073.64
				
					Imports System
Imports uCalcSoftware
Public Module Program
   Public Sub Main()
      Dim uc As New uCalc()
      '// Define a function for the standard loan payment formula
      uc.DefineFunction("LoanPmt(rate, nper, pv) = Int((rate * pv) / (1 - (1 + rate)^-nper)*100)/100")
      
      '// Define variables for the calculation
      uc.DefineVariable("monthly_rate = 0.05 / 12") '// 5% annual rate
      uc.DefineVariable("periods = 30 * 12")      '// 30 years
      uc.DefineVariable("loan_amount = 200000")   '// $200,000
      
      Console.WriteLine(uc.EvalStr("LoanPmt(monthly_rate, periods, loan_amount)"))
   End Sub
End Module
				
			
1073.64
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
				
					#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();
}
				
			
--- 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
				
			
--- Efficient: Parse() once, then Evaluate() in a loop ---
10
40
90
160
250
360
490
640
810
1000