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.

Eval

Method

Product: 

Fast Math Parser

Class: 

uCalcBase

Parses and evaluates a string expression in a single step, returning a numeric result.

Syntax

Eval(string)

Parameters

expression
string
The string containing the mathematical, or logical expression to parse and evaluate.

Return

double

The double-precision floating-point result of the evaluated expression.

Remarks

The Eval method provides a convenient one-step shortcut to parse and immediately evaluate an expression, returning its result. It is ideal for scenarios where an expression is evaluated only once and performance is not the primary concern.

When to Use Eval

Eval is the simplest way to get a result from a string. However, it performs two operations internally: parsing and evaluation. If you need to evaluate the same expression multiple times (e.g., in a loop), it is significantly more efficient to use the two-step approach:

  1. Call uCalc.Parse once to create a compiled Expression object.
  2. Call Expression.Evaluate repeatedly on that object.

This avoids the overhead of re-parsing the string on every iteration.

⚠️ Return Type Limitation

This function is specifically designed for expressions that yield a simple numeric result. It always returns a double.

  • If the expression's natural result is another numeric type (like Int32 or Boolean), the value will be converted to a double.
  • If the expression yields a non-numeric or complex type (like a String or Complex number), the return value will be meaningless. No error is raised in this case.

For expressions that return non-numeric types or to get a string representation of any result, use the more versatile uCalc.EvalStr method instead.

Supported Syntax

The expression string can contain any combination of numbers, variables, and calls to built-in or user-defined Functions and Operators.

Comparative Analysis

  • vs. C# DataTable.Compute: A common workaround in .NET for evaluating strings is using the Compute method of a DataTable. While it works for simple arithmetic, it is far less powerful, significantly slower, and lacks the rich feature set of uCalc (custom functions, complex numbers, transformers, etc.).
  • vs. Other Parsing Libraries (e.g., NCalc, Flee): Many libraries offer similar Evaluate() methods. uCalc's Eval differentiates itself by being part of a much larger, cohesive engine. The expression can leverage custom data types, operators, and transformation rules defined within the same uCalc instance, offering greater flexibility and power than single-purpose evaluation libraries.
  • vs. Parse + Evaluate: As mentioned, Eval is for convenience. The two-step process is for performance-critical applications.

Examples

Quickly evaluates several basic mathematical and function-based expressions.
				
					using uCalcSoftware;

var uc = new uCalc();
// Eval provides a direct way to compute results from strings.
Console.WriteLine(uc.Eval("1+1"));
Console.WriteLine(uc.Eval("5*(3+9)^2"));
Console.WriteLine(uc.Eval("Sqrt(16) + Sin(0)"));
Console.WriteLine(uc.Eval("Length('This is a test')"));
				
			
2
720
4
14
				
					#include <iostream>
#include "uCalc.h"

using namespace std;
using namespace uCalcSoftware;

int main() {
   uCalc uc;
   // Eval provides a direct way to compute results from strings.
   cout << uc.Eval("1+1") << endl;
   cout << uc.Eval("5*(3+9)^2") << endl;
   cout << uc.Eval("Sqrt(16) + Sin(0)") << endl;
   cout << uc.Eval("Length('This is a test')") << endl;
}
				
			
2
720
4
14
				
					Imports System
Imports uCalcSoftware
Public Module Program
   Public Sub Main()
      Dim uc As New uCalc()
      '// Eval provides a direct way to compute results from strings.
      Console.WriteLine(uc.Eval("1+1"))
      Console.WriteLine(uc.Eval("5*(3+9)^2"))
      Console.WriteLine(uc.Eval("Sqrt(16) + Sin(0)"))
      Console.WriteLine(uc.Eval("Length('This is a test')"))
   End Sub
End Module
				
			
2
720
4
14
Calculates simple interest by defining variables and then evaluating an expression string that uses them.
				
					using uCalcSoftware;

var uc = new uCalc();
// Define variables representing configuration or user inputs.
uc.DefineVariable("principal = 20000");
uc.DefineVariable("annual_rate = 0.0575");
uc.DefineVariable("years = 4");

// Evaluate an expression combining these variables.
var interest = uc.Eval("principal * annual_rate * years");
Console.WriteLine($"Total Interest: {interest}");
				
			
Total Interest: 4600
				
					#include <iostream>
#include "uCalc.h"

using namespace std;
using namespace uCalcSoftware;

int main() {
   uCalc uc;
   // Define variables representing configuration or user inputs.
   uc.DefineVariable("principal = 20000");
   uc.DefineVariable("annual_rate = 0.0575");
   uc.DefineVariable("years = 4");

   // Evaluate an expression combining these variables.
   auto interest = uc.Eval("principal * annual_rate * years");
   cout << "Total Interest: " << interest << endl;
}
				
			
Total Interest: 4600
				
					Imports System
Imports uCalcSoftware
Public Module Program
   Public Sub Main()
      Dim uc As New uCalc()
      '// Define variables representing configuration or user inputs.
      uc.DefineVariable("principal = 20000")
      uc.DefineVariable("annual_rate = 0.0575")
      uc.DefineVariable("years = 4")
      
      '// Evaluate an expression combining these variables.
      Dim interest = uc.Eval("principal * annual_rate * years")
      Console.WriteLine($"Total Interest: {interest}")
   End Sub
End Module
				
			
Total Interest: 4600
Evaluating expressions
				
					using uCalcSoftware;

var uc = new uCalc();
// See EvalStr for more examples.

Console.WriteLine(uc.Eval("1+1"));
Console.WriteLine(uc.Eval("5*(3+9)^2"));
Console.WriteLine(uc.Eval("Length('This is a test')"));
				
			
2
720
14
				
					#include <iostream>
#include "uCalc.h"

using namespace std;
using namespace uCalcSoftware;

int main() {
   uCalc uc;
   // See EvalStr for more examples.

   cout << uc.Eval("1+1") << endl;
   cout << uc.Eval("5*(3+9)^2") << endl;
   cout << uc.Eval("Length('This is a test')") << endl;
}
				
			
2
720
14
				
					Imports System
Imports uCalcSoftware
Public Module Program
   Public Sub Main()
      Dim uc As New uCalc()
      '// See EvalStr for more examples.
      
      Console.WriteLine(uc.Eval("1+1"))
      Console.WriteLine(uc.Eval("5*(3+9)^2"))
      Console.WriteLine(uc.Eval("Length('This is a test')"))
   End Sub
End Module
				
			
2
720
14
A minimal example defining a function inline to calculate the area of a rectangle.
				
					using uCalcSoftware;

var uc = new uCalc();
uc.DefineFunction("Area(length, width) = length * width");
Console.WriteLine(uc.Eval("Area(4, 5)"));
				
			
20
				
					#include <iostream>
#include "uCalc.h"

using namespace std;
using namespace uCalcSoftware;

int main() {
   uCalc uc;
   uc.DefineFunction("Area(length, width) = length * width");
   cout << uc.Eval("Area(4, 5)") << endl;
}
				
			
20
				
					Imports System
Imports uCalcSoftware
Public Module Program
   Public Sub Main()
      Dim uc As New uCalc()
      uc.DefineFunction("Area(length, width) = length * width")
      Console.WriteLine(uc.Eval("Area(4, 5)"))
   End Sub
End Module
				
			
20