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.
Using Built-in Functions
Product:
Class:
Explores how to use the rich library of built-in functions for math, string manipulation, and logic within uCalc expressions.
Remarks
Built-in Functions
uCalc comes with a comprehensive library of built-in functions that will feel familiar to developers coming from C++, C#, Visual Basic, or spreadsheet applications like Excel. These functions provide the core toolkit for performing mathematical calculations, string manipulation, and logical operations directly within an expression string.
You can call any built-in function by its name, followed by its arguments in parentheses, just as you would in a standard programming language.
// A simple expression combining math and string functionsuc.EvalStr("Abs(-10) + Length('hello')") // Output: 15For a complete list of all functions, see the Functions and Operators reference section.
Function Categories
Built-in functions are grouped into several logical categories:
📐 Math Functions
This is the largest category, providing a full suite of trigonometric, logarithmic, rounding, and power functions. It includes everything from basic Abs() and Sqrt() to advanced operations like Tgamma() and Hypot().
Console.WriteLine(uc.Eval("Sin(PI/2) * Pow(2, 10)")); // Output: 1024
🧵 String Functions
This category includes functions for creating, modifying, and inspecting strings. You can perform operations like concatenation (+), finding substrings (IndexOf), changing case (UCase), and more.
Console.WriteLine(uc.EvalStr("SubStr('Hello World', 6, 5)")); // Output: World
🧩 Logic & Specialized Functions
This category contains functions for conditional logic, type conversion, and interacting with the engine itself. The most important of these is IIf(), uCalc's equivalent of the ternary operator (condition ? true_part : false_part). It uses lazy evaluation via ByExpr arguments, meaning it only evaluates the branch that is chosen.
Console.WriteLine(uc.EvalStr("IIf(10 > 5, 'Greater', 'Lesser')")); // Output: Greater
💡 Why uCalc? (Comparative Analysis)
vs. Statically Compiled Languages (C#, C++)
The primary advantage is dynamism. In C# or C++, function calls are resolved at compile time. With uCalc, an entire expression, including calls to multiple built-in functions, can be constructed as a string at runtime. This is fundamental for applications where the logic is not known ahead of time, such as:
- Report Generators: Users can type formulas like
Avg(sales) * (1 + TAX_RATE)into a text field. - Game Scripting: Define character behaviors or game rules in configuration files.
- Scientific Modeling: Allow researchers to input complex mathematical formulas without needing to recompile the application.
vs. Other Evaluators (Excel, SQL)
While spreadsheet and database languages also have built-in functions, uCalc provides a more complete programming environment. You are not limited to a single formula in a cell or a query. You can define variables, create custom functions, and build complex, multi-step logic that leverages the built-in function library as its foundation.
Examples
A simple example demonstrating a few of the most common math, logic, and string functions.
using uCalcSoftware;
var uc = new uCalc();
Console.WriteLine($"Square root of 81 is: {uc.Eval("Sqrt(81)")}");
Console.WriteLine($"Is 10 greater than 5? {uc.EvalStr("IIf(10 > 5, 'Yes', 'No')")}");
Console.WriteLine($"String length: {uc.Eval("Length('uCalc rocks!')")}");
Square root of 81 is: 9
Is 10 greater than 5? Yes
String length: 12 using uCalcSoftware; var uc = new uCalc(); Console.WriteLine($"Square root of 81 is: {uc.Eval("Sqrt(81)")}"); Console.WriteLine($"Is 10 greater than 5? {uc.EvalStr("IIf(10 > 5, 'Yes', 'No')")}"); Console.WriteLine($"String length: {uc.Eval("Length('uCalc rocks!')")}");
#include
#include "uCalc.h"
using namespace std;
using namespace uCalcSoftware;
int main() {
uCalc uc;
cout << "Square root of 81 is: " << uc.Eval("Sqrt(81)") << endl;
cout << "Is 10 greater than 5? " << uc.EvalStr("IIf(10 > 5, 'Yes', 'No')") << endl;
cout << "String length: " << uc.Eval("Length('uCalc rocks!')") << endl;
}
Square root of 81 is: 9
Is 10 greater than 5? Yes
String length: 12 #include <iostream> #include "uCalc.h" using namespace std; using namespace uCalcSoftware; int main() { uCalc uc; cout << "Square root of 81 is: " << uc.Eval("Sqrt(81)") << endl; cout << "Is 10 greater than 5? " << uc.EvalStr("IIf(10 > 5, 'Yes', 'No')") << endl; cout << "String length: " << uc.Eval("Length('uCalc rocks!')") << endl; }
Imports System
Imports uCalcSoftware
Public Module Program
Public Sub Main()
Dim uc As New uCalc()
Console.WriteLine($"Square root of 81 is: {uc.Eval("Sqrt(81)")}")
Console.WriteLine($"Is 10 greater than 5? {uc.EvalStr("IIf(10 > 5, 'Yes', 'No')")}")
Console.WriteLine($"String length: {uc.Eval("Length('uCalc rocks!')")}")
End Sub
End Module
Square root of 81 is: 9
Is 10 greater than 5? Yes
String length: 12 Imports System Imports uCalcSoftware Public Module Program Public Sub Main() Dim uc As New uCalc() Console.WriteLine($"Square root of 81 is: {uc.Eval("Sqrt(81)")}") Console.WriteLine($"Is 10 greater than 5? {uc.EvalStr("IIf(10 > 5, 'Yes', 'No')")}") Console.WriteLine($"String length: {uc.Eval("Length('uCalc rocks!')")}") End Sub End Module
Internal Test: Verifies that a built-in function using `ByHandle` arguments can correctly modify the state of variables passed to it.
using uCalcSoftware;
var uc = new uCalc();
uc.DefineVariable("a = 10");
uc.DefineVariable("b = 20");
Console.WriteLine($"Before - a: {uc.Eval("a")}, b: {uc.Eval("b")}");
// The built-in Swap() function uses ByHandle to modify variables directly.
uc.Eval("Swap(a, b)");
Console.Write("After - a: "); Console.Write(uc.Eval("a")); Console.Write(", b: "); Console.Write(uc.Eval("b"));
Before - a: 10, b: 20
After - a: 20, b: 10 using uCalcSoftware; var uc = new uCalc(); uc.DefineVariable("a = 10"); uc.DefineVariable("b = 20"); Console.WriteLine($"Before - a: {uc.Eval("a")}, b: {uc.Eval("b")}"); // The built-in Swap() function uses ByHandle to modify variables directly. uc.Eval("Swap(a, b)"); Console.Write("After - a: "); Console.Write(uc.Eval("a")); Console.Write(", b: "); Console.Write(uc.Eval("b"));
#include
#include "uCalc.h"
using namespace std;
using namespace uCalcSoftware;
int main() {
uCalc uc;
uc.DefineVariable("a = 10");
uc.DefineVariable("b = 20");
cout << "Before - a: " << uc.Eval("a") << ", b: " << uc.Eval("b") << endl;
// The built-in Swap() function uses ByHandle to modify variables directly.
uc.Eval("Swap(a, b)");
cout << "After - a: " << uc.Eval("a") << ", b: " << uc.Eval("b");
}
Before - a: 10, b: 20
After - a: 20, b: 10 #include <iostream> #include "uCalc.h" using namespace std; using namespace uCalcSoftware; int main() { uCalc uc; uc.DefineVariable("a = 10"); uc.DefineVariable("b = 20"); cout << "Before - a: " << uc.Eval("a") << ", b: " << uc.Eval("b") << endl; // The built-in Swap() function uses ByHandle to modify variables directly. uc.Eval("Swap(a, b)"); cout << "After - a: " << uc.Eval("a") << ", b: " << uc.Eval("b"); }
Imports System
Imports uCalcSoftware
Public Module Program
Public Sub Main()
Dim uc As New uCalc()
uc.DefineVariable("a = 10")
uc.DefineVariable("b = 20")
Console.WriteLine($"Before - a: {uc.Eval("a")}, b: {uc.Eval("b")}")
'// The built-in Swap() function uses ByHandle to modify variables directly.
uc.Eval("Swap(a, b)")
Console.Write("After - a: ")
Console.Write(uc.Eval("a"))
Console.Write(", b: ")
Console.Write(uc.Eval("b"))
End Sub
End Module
Before - a: 10, b: 20
After - a: 20, b: 10 Imports System Imports uCalcSoftware Public Module Program Public Sub Main() Dim uc As New uCalc() uc.DefineVariable("a = 10") uc.DefineVariable("b = 20") Console.WriteLine($"Before - a: {uc.Eval("a")}, b: {uc.Eval("b")}") '// The built-in Swap() function uses ByHandle to modify variables directly. uc.Eval("Swap(a, b)") Console.Write("After - a: ") Console.Write(uc.Eval("a")) Console.Write(", b: ") Console.Write(uc.Eval("b")) End Sub End Module