uCalc Fast Math Parser Overview

 

See Also: Feature List, Operators and Functions, What's New in version 2.96

 

uCalc Fast Math Parser is a 32-bit DLL component for Windows 98/Me/XP/Vista, which allows your program to evaluate expressions that are defined at runtime.  This component was carefully designed to 1) perform computations very rapidly 2) be easy to implement 3) be sturdy and reliable, and 4) allow maximum flexibility in parsing and evaluating expressions.  The following links contain specific details for implementing uCalc with supported compilers:  C++ Builder, C#, Delphi, PowerBASIC, Visual Basic (classic), Visual Basic .NET, Visual C++, other compilers.

 

 

The basics

 

The following examples show you how to quickly get started using uCalc FMP.  After browsing through the examples below and giving them a try, you will then want to look at the various other topics of this help file to discover additional ways of using uCalc.  See also the demo program.

 

 

Example 1:  Simple evaluation

 

The ucEval() function evaluates an expression, and returns a numeric result, all in one step.  ucEvalStr() is similar, but it can work with either numeric or non-numeric expressions, returning the result as a string.  This example lets the user enter an expression (for instance, something like: 6+4*5/2).  The expression is evaluated, and a message box displays the result, which is 16.

 

C++ Builder

String UserExpression;

UserExpression = InputBox("uCalc demo", "Enter an expression", "6+4*5/2");

ShowMessage(ucEvalStr(UserExpression));

 

C#

// Create a TextBox named textBox1 on your form first.

// Then enter an expression such as: 6+4*5/2

MessageBox.Show(uc.ucEvalStr(textBox1.Text));

 

Delphi

var

   UserExpression: string;

begin

   UserExpression := InputBox('uCalc demo', 'Enter an expression', '6+4*5/2');

   ShowMessage(ucEvalStr(UserExpression));

end;

 

PowerBASIC

UserExpression$ = InputBox$("Enter an expression",, "6+4*5/2")

MsgBox ucEvalStr(UserExpression$)

 

Visual Basic (classic)

UserExpression$ = InputBox("Enter an expression",, "6+4*5/2")

MsgBox ucEvalStr(UserExpression$)

 

Visual Basic .NET

Dim UserExpression As String

UserExpression = InputBox("Enter an expression", , "6+4*5/2")

MsgBox(ucEvalStr(UserExpression))

 

Visual C++

// This example is for a project designed as a Console Application

char UserExpression[80];

printf("Enter an expression (such as 6+4*5/2): ");

scanf("%s", UserExpression);

printf(ucEvalStr(UserExpression));

printf("\n");

 

 

Example 2:  Fast evaluation millions of times in a loop

 

The following example is an adaptation of the Summation example found in the demo program that comes with uCalc FMP.  It demonstrates the speed of using ucParse() first, and then ucEvaluate() inside a calculation loop that is repeated many times.  It performs a summation on a user-supplied math expression, such as:  x^2+5*x-10, from 1 to 2000000.

 

C++ Builder

long double x = 1, SumTotal = 0;

DWORD ExprHandle, xHandle;

 

xHandle = ucDefineVariable("x", &x);

ExprHandle = ucParse(InputBox("uCalc demo", "Enter an expression", "x^2+5*x-10"));

 

while (x <= 2000000)

{

   SumTotal = SumTotal + ucEvaluate(ExprHandle);

   x++;

}

 

ShowMessage(FloatToStr(SumTotal));

ucReleaseItem(ExprHandle);

ucReleaseItem(xHandle);

 

C#

// This C# example requires a TextBox named textBox1.

// Enter an expression such as x^2+5*x-10 in that text box.

int ExprHandle, xHandle;

double x = 1, SumTotal = 0;  

  

xHandle = uc.ucDefineVariable("x");

ExprHandle = uc.ucParse(textBox1.Text);

 

while (x <= 2000000)

{

   uCalcNET.uCalcNET.ucSetVariableValue(xHandle, x);

   // See the actual demo for a C# alternative to the above line, which is faster

   SumTotal = SumTotal + uCalcNET.uCalcNET.ucEvaluate(ExprHandle);

   x++;

}

 

MessageBox.Show(System.Convert.ToString(SumTotal));

uCalcNET.uCalcNET.ucReleaseItem(ExprHandle);

uCalcNET.uCalcNET.ucReleaseItem(xHandle);

 

Delphi

var

   ExprHandle, xHandle: Longword;

   x, SumTotal: Extended;

   UserExpression: string;

begin

   x := 1;

   SumTotal := 0;

 

   UserExpression := InputBox('uCalc demo', 'Enter an expression', 'x^2+5*x-10');

   xHandle := ucDefineVariable('x', @x);

   ExprHandle := ucParse(UserExpression);

 

   while x <= 2000000 do

   begin

     SumTotal := SumTotal + ucEvaluate(ExprHandle);

     x := x + 1;

   end;

 

   ShowMessage(FloatToStr(SumTotal));

 

   ucReleaseItem(ExprHandle);

   ucReleaseItem(xHandle);

end;

 

PowerBASIC

Dim UserExpression As String

Dim ExprHandle As Dword, xHandle As Dword

Dim x As Extended, SumTotal As Extended

  

UserExpression = InputBox$("Enter an expression",, "x^2+5*x-10")

xHandle = ucDefineVariable("x", VarPtr(x))

ExprHandle = ucParse(UserExpression)

  

For x = 1 To 2000000

   SumTotal = SumTotal + ucEvaluate(ExprHandle)

Next

 

MsgBox Str$(SumTotal)

 

ucReleaseItem ExprHandle

ucReleaseItem xHandle

 

Visual Basic (classic)

Dim ExprHandle As Long, xHandle As Long

Dim x As Double, SumTotal As Double

Dim UserExpression As String

  

UserExpression = InputBox("Enter an expression", , "x^2+5*x-10")

xHandle = ucDefineVariable("x", VarPtr(x))

ExprHandle = ucParse(UserExpression)

  

For x = 1 To 2000000

   SumTotal = SumTotal + ucEvaluate(ExprHandle)

Next

  

MsgBox Str$(SumTotal)

   

ucReleaseItem ExprHandle

ucReleaseItem xHandle

 

Visual Basic .NET

Dim ExprHandle As Integer, xHandle As Integer

Dim x As Double, SumTotal As Double

Dim UserExpression As String

 

UserExpression = InputBox("Enter an expression", , "x^2+5*x-10")

xHandle = ucDefineVariable("x")

ExprHandle = ucParse(UserExpression)

       

For x = 1 To 2000000

   ucSetVariableValue(xHandle, x)

   SumTotal = SumTotal + ucEvaluate(ExprHandle)

Next

 

MsgBox Str(SumTotal)

 

ucReleaseItem(ExprHandle)

ucReleaseItem(xHandle)

 

Visual C++

// This VC++ example requires an Edit Box named IDC_EDIT1.

// Enter an expression such as x^2+5*x-10 in that box.

DWORD ExprHandle, xHandle;  

double x = 1, SumTotal = 0;

char UserExpression[200];

char txtSumTotal[200];

 

GetDlgItemText(IDC_EDIT1, UserExpression, 200);  

 

xHandle = ucDefineVariable("x", &x);

ExprHandle = ucParse(UserExpression); 

 

while (x <= 2000000)

{

   SumTotal = SumTotal + ucEvaluate(ExprHandle);

   x++;

}

 

gcvt(SumTotal, 15, txtSumTotal);

MessageBox(txtSumTotal);  

 

ucReleaseItem(ExprHandle);

ucReleaseItem(xHandle);