Your First Expression

Learn how to parse and evaluate your first mathematical or logical expression using uCalc's core functions.

Product: 

Class: 

Warning

uCalc API Preview Release Notice:The uCalc engine has successfully transitioned to modern cross-platform environments.The next phase envolves some structural changes, performance optimizations, and API refinements.The API is subject to breaking changes prior to the stable release. Please evaluate the preview version thoroughly before production use.

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

One of the easiest ways 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.

using uCalcSoftware;

var uc = new uCalc();

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:

  1. Parses: It reads the string "5 * 10" and breaks it into tokens: the number 5, the multiplication operator *, and the number 10.
  2. Evaluates: It executes the parsed instruction, calculating the result 50.
  3. 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.

Another easy way is the using the following syntax:

var myExpr = new uCalc.Expression("5+4");Console.WriteLine(myExpr); // returns 9

Here, Console.WriteLine(myExpr); is a shortcut for Console.WriteLine(myExpr.EvaluateStr());

💡 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.Compute method. 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.

ID: 1177

				
					using uCalcSoftware;

var uc = new uCalc();
Console.WriteLine(uc.EvalStr("10 * 5 + 3"));
				
			
53
				
					#include <iostream>
#include "uCalc.h"

using namespace std;
using namespace uCalcSoftware;

int main() {
   uCalc uc;
   cout << uc.EvalStr("10 * 5 + 3") << endl;
}
				
			
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
				
			
53
Calculating a simple percentage for a real-world financial scenario.

ID: 1178

				
					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
				
					#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;
}
				
			
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
				
			
Discounted price: 63.75
Simplified syntax for evaluating an expression

ID: 1467

				
					using uCalcSoftware;

var myExpr = new uCalc.Expression("5+4");
Console.WriteLine(myExpr);

myExpr = "10+20";
Console.WriteLine(myExpr);
				
			
9
30
				
					#include <iostream>
#include "uCalc.h"

using namespace std;
using namespace uCalcSoftware;

int main() {
   uCalc::Expression myExpr("5+4");
   cout << myExpr << endl;

   myExpr = "10+20";
   cout << myExpr << endl;
}
				
			
9
30
				
					Imports System
Imports uCalcSoftware
Public Module Program
   Public Sub Main()
      
      
      Dim myExpr As New uCalc.Expression("5+4")
      Console.WriteLine(myExpr)
      
      myExpr = "10+20"
      Console.WriteLine(myExpr)
   End Sub
End Module
				
			
9
30
Internal Test: Verify correct order of operations with mixed operators and parentheses.

ID: 1179

				
					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
				
					#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;
}
				
			
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
				
			
23