uCalc Fast Math Parser Overview
See Also: Feature List, Operators and Functions, What's New in version 3
uCalc Fast Math Parser is a DLL component for Windows 98/Me/XP/Vista/7/8, 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.
32-bit and 64-bit
uCalc FMP comes with DLLs for use with 32-bit and 64-bit applications. However, the current 64-bit DLL is a provisional one. It is implemented as a separate process that calls the 32-bit DLL using IPC (you don't need to worry about any of these details for it to work). As such, instead of being even faster than the 32-bit version, it is actually slower, and takes up more memory. It does not take advantage of special properties of being a 64-bit DLL. It also has limitations and quirks. For instance, it cannot handle error handling callbacks. And for function callbacks, you must use only the native option (which is actually the preferred option even in 32-bit). Please note that some examples in the demo (the same one is used for 32-bit and 64-bit) feature the error handling callback and nonnative callback functions; those should be avoided when you run the demo. If you shut down your app abruptly instead of normally, the 64-bit DLL process will remain in memory. The main purpose of this implementation is to allow you to be able to use most of uCalc FMP's functionality in your 64-bit apps right now, as a new 64-bit DLL is rewritten from the ground up to make full use of 64-bit benefits.
IMPORTANT: There is a different set of include files for use with 64-bit applications. These include files contain "x64" in the file name. Be sure to use those instead. For instance, to compile a 64-bit application with C#, use uCalcCSx64.cs instead of uCalcCS.cs (that one is for 32-bit apps only).
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.
String UserExpression; UserExpression
= InputBox("uCalc
demo", "Enter an expression", "6+4*5/2"); ShowMessage(ucEvalStr(UserExpression)); |
// Create a TextBox
named textBox1 on your form first. // Then enter an expression such as:
6+4*5/2 MessageBox.Show(uCalc.ucEvalStr(textBox1.Text)); |
var UserExpression: string; begin UserExpression := InputBox('uCalc demo', 'Enter an expression', '6+4*5/2'); ShowMessage(ucEvalStr(UserExpression)); end; |
UserExpression$
= InputBox$("Enter an expression",,
"6+4*5/2") MsgBox
ucEvalStr(UserExpression$) |
UserExpression$
= InputBox("Enter an expression",,
"6+4*5/2") MsgBox
ucEvalStr(UserExpression$) |
Dim UserExpression As
String UserExpression
= InputBox("Enter an expression", ,
"6+4*5/2") MsgBox(ucEvalStr(UserExpression)) |
// 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.
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); |
// 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
= uCalc.ucDefineVariable("x"); ExprHandle
= uCalc.ucParse(textBox1.Text); while (x <= 2000000) { uCalc.ucSetVariableValue(xHandle,
x); // See the actual demo for a C# alternative to the above
line, which is faster SumTotal = SumTotal + uCalc.ucEvaluate(ExprHandle); x++; } MessageBox.Show(System.Convert.ToString(SumTotal)); uCalc.ucReleaseItem(ExprHandle); uCalc.ucReleaseItem(xHandle); |
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; |
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 |
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 |
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) |
// 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); |