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.
Your First Expression
Product:
Class:
Learn how to parse and evaluate your first mathematical or logical expression using uCalc's core functions.
Remarks
✅ Your First Expression
Welcome to your first step in using uCalc! At its core, uCalc is an engine that can understand and compute expressions provided as plain text strings. This is the foundation for everything else the library can do.
The EvalStr Method
The easiest way to evaluate an expression is with the EvalStr method. It's a powerful, one-step function that takes a string, processes it, and returns the result as a string. It's the perfect starting point because it's both versatile and safe.
Let's try it with a simple arithmetic expression. In all our examples, the uc object is an implicitly available instance of the main uCalc class.
Console.Write("The result of 5 * 10 is: ");Console.WriteLine(uc.EvalStr("5 * 10"));When this code runs, uCalc performs three actions behind the scenes:
- Parses: It reads the string
"5 * 10"and breaks it into tokens: the number5, the multiplication operator*, and the number10. - Evaluates: It executes the parsed instruction, calculating the result
50. - Returns String: It converts the result to a string (
"50") and returns it.
Because EvalStr can handle any data type and returns errors as plain text messages, it's the most robust method for handling dynamic or user-provided input.
💡 Why uCalc? (Comparative Analysis)
How would you normally evaluate a string expression without a dedicated library?
In .NET (C#/VB): A common workaround is using the
DataTable.Computemethod. This is notoriously slow, limited to simple arithmetic, and lacks features like custom functions or advanced operators. uCalc's EvalStr is part of a high-performance, full-featured parsing engine that is orders of magnitude more powerful.In Scripting Languages (JavaScript/Python): These languages have a built-in
eval()function. While powerful, they are often a security risk because they can execute arbitrary code. uCalc's engine is a secure sandbox; it can only execute the functions and access the variables you have explicitly defined, preventing code injection vulnerabilities.
Now that you've seen the basics, let's explore a few more examples.
Examples
Evaluating a basic arithmetic expression with multiple operators.
using uCalcSoftware;
var uc = new uCalc();
Console.WriteLine(uc.EvalStr("10 * 5 + 3"));
53 using uCalcSoftware; var uc = new uCalc(); Console.WriteLine(uc.EvalStr("10 * 5 + 3"));
#include
#include "uCalc.h"
using namespace std;
using namespace uCalcSoftware;
int main() {
uCalc uc;
cout << uc.EvalStr("10 * 5 + 3") << endl;
}
53 #include <iostream> #include "uCalc.h" using namespace std; using namespace uCalcSoftware; int main() { uCalc uc; cout << uc.EvalStr("10 * 5 + 3") << endl; }
Imports System
Imports uCalcSoftware
Public Module Program
Public Sub Main()
Dim uc As New uCalc()
Console.WriteLine(uc.EvalStr("10 * 5 + 3"))
End Sub
End Module
53 Imports System Imports uCalcSoftware Public Module Program Public Sub Main() Dim uc As New uCalc() Console.WriteLine(uc.EvalStr("10 * 5 + 3")) End Sub End Module
Calculating a simple percentage for a real-world financial scenario.
using uCalcSoftware;
var uc = new uCalc();
// Calculate a 15% discount on a price of $75
Console.Write("Discounted price: ");
Console.WriteLine(uc.EvalStr("75 * (1 - 0.15)"));
Discounted price: 63.75 using uCalcSoftware; var uc = new uCalc(); // Calculate a 15% discount on a price of $75 Console.Write("Discounted price: "); Console.WriteLine(uc.EvalStr("75 * (1 - 0.15)"));
#include
#include "uCalc.h"
using namespace std;
using namespace uCalcSoftware;
int main() {
uCalc uc;
// Calculate a 15% discount on a price of $75
cout << "Discounted price: ";
cout << uc.EvalStr("75 * (1 - 0.15)") << endl;
}
Discounted price: 63.75 #include <iostream> #include "uCalc.h" using namespace std; using namespace uCalcSoftware; int main() { uCalc uc; // Calculate a 15% discount on a price of $75 cout << "Discounted price: "; cout << uc.EvalStr("75 * (1 - 0.15)") << endl; }
Imports System
Imports uCalcSoftware
Public Module Program
Public Sub Main()
Dim uc As New uCalc()
'// Calculate a 15% discount on a price of $75
Console.Write("Discounted price: ")
Console.WriteLine(uc.EvalStr("75 * (1 - 0.15)"))
End Sub
End Module
Discounted price: 63.75 Imports System Imports uCalcSoftware Public Module Program Public Sub Main() Dim uc As New uCalc() '// Calculate a 15% discount on a price of $75 Console.Write("Discounted price: ") Console.WriteLine(uc.EvalStr("75 * (1 - 0.15)")) End Sub End Module
Internal Test: Verify correct order of operations with mixed operators and parentheses.
using uCalcSoftware;
var uc = new uCalc();
// This tests the precedence of power (^), multiplication (*), and addition (+).
// The correct evaluation is 5 + (2 * (3^2)) -> 5 + (2 * 9) -> 5 + 18 -> 23.
Console.WriteLine(uc.EvalStr("5 + 2 * 3^2"));
23 using uCalcSoftware; var uc = new uCalc(); // This tests the precedence of power (^), multiplication (*), and addition (+). // The correct evaluation is 5 + (2 * (3^2)) -> 5 + (2 * 9) -> 5 + 18 -> 23. Console.WriteLine(uc.EvalStr("5 + 2 * 3^2"));
#include
#include "uCalc.h"
using namespace std;
using namespace uCalcSoftware;
int main() {
uCalc uc;
// This tests the precedence of power (^), multiplication (*), and addition (+).
// The correct evaluation is 5 + (2 * (3^2)) -> 5 + (2 * 9) -> 5 + 18 -> 23.
cout << uc.EvalStr("5 + 2 * 3^2") << endl;
}
23 #include <iostream> #include "uCalc.h" using namespace std; using namespace uCalcSoftware; int main() { uCalc uc; // This tests the precedence of power (^), multiplication (*), and addition (+). // The correct evaluation is 5 + (2 * (3^2)) -> 5 + (2 * 9) -> 5 + 18 -> 23. cout << uc.EvalStr("5 + 2 * 3^2") << endl; }
Imports System
Imports uCalcSoftware
Public Module Program
Public Sub Main()
Dim uc As New uCalc()
'// This tests the precedence of power (^), multiplication (*), and addition (+).
'// The correct evaluation is 5 + (2 * (3^2)) -> 5 + (2 * 9) -> 5 + 18 -> 23.
Console.WriteLine(uc.EvalStr("5 + 2 * 3^2"))
End Sub
End Module
23 Imports System Imports uCalcSoftware Public Module Program Public Sub Main() Dim uc As New uCalc() '// This tests the precedence of power (^), multiplication (*), and addition (+). '// The correct evaluation is 5 + (2 * (3^2)) -> 5 + (2 * 9) -> 5 + 18 -> 23. Console.WriteLine(uc.EvalStr("5 + 2 * 3^2")) End Sub End Module