TFunctionParser™ is a parser for analyzing and evaluating mathematical functions.
The function term is a user given string like 'sin(x)*x'. The function term can be
changed anytime.
TFunctionParser provides about 100 mathematical intrinsic functions and operations.
The user can define functions, constants and variable names himself.
TFunctionParser was formerly a Delphi component, but with different Delphi versions the binaries were not compatible. Thus we had to compile the sources for each Delphi version (in some cases even the sources were not compatible). So we thought releasing a DLL that encapsulates TFunctionParser would be a good idea and we came to COM/Automation.
Here is Microsoft's Definition:
COM is a platform-independent, distributed, object-oriented system for creating binary software components that can interact. COM is the foundation technology for Microsoft's OLE (compound documents) and ActiveX (Internet-enabled components) technologies, as well as others.
Automation is a COM-based technology that allows developers to create custom applications to automate frequent tasks.
If this is your first contact with COM/Automation you might be a little confused - but you will soon get familiar with it.
Thus TFunctionParser is a software that can be used by software developers to build their own applications embedding a powerful and comprehensive mathematical parser for quick and easy analyzing and evaluating mathematical functions.
Every programming or scripting language that supports COM can use TFunctionParser: Delphi, C, C++, C#, PHP, Perl, VBScript, JavaScript, VBA, Java, PowerShell, ...
TFunctionParser provides the following features:
Though floating point numbers are communicated with the automation client as double type (64 bit), TFunctionParser calculates internally with extended numbers (80 bit). This is because the floating point unit uses extended numbers, but Automation/COM supports only double as the most precise floating point data type.
Operation System: Any Windows system
TFunctionParser is available for 32 bit and 64 bit operating systems.
You need the variant which matches your client. That is, if you are running a 32 bit operating system, you will need TFunctionParser 32 bit.
If you are running a 64 bit operating system, things are a bit more complex. Usually (for clients like Windows Scripting Host) you need TFunctionParser 64 bit. But if your client is still a 32 bit application, you'll need the 32 bit variant. Please keep in mind to consider this also for the installation.
TFunctionParser 7.4 is Shareware.
You may test it for a period of 30 days.
It may be copied and distributed unmodified (complete ZIP-file, no single files).
If you use it in your non-commercial applications you must register it. The license fee for non-commercial use is 20 EUR. If you want to use TFunctionParser for commercial purposes please contact us.
Registered users of one of the former versions get a discount.
You will obtain a special edition without the automatic initial info screen. This special edition additionally contains a DLL interface.
If you are not a registered user, the infoscreen will appear each time an instance of TFunctionParser is created. Unregistered use is limited to 30 days. Please note that unregistered use after this evalution period will violate copyright law and international treaties and you will make yourself liable to prosecution.
Contact:
eMail: mhgs@MHGSoft.de URL:
http://www.MHGSoft.de
You are obliged to quote our copyright in all your applications and programs.
When ordering your personal edition please confirm that you read this file and agree to the license conditions. Don't forget your eMail address.
If you find any bug, please don't hesitate to inform us. Details (functions, arguments, ...)
would be helpful.
Any suggestions, questions, and comments are welcome.
---------------------------------- THE LEGAL DISCLAIMER ---------------------------------- THE INFORMATION AND CODE PROVIDED HEREUNDER (COLLECTIVELY REFERRED TO AS "SOFTWARE") IS PROVIDED AS IS WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MHGS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MHGS HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
The file MHGSPARS.ZIP contains the following files:
MHGSPARS.DLL | Server-DLL |
---|---|
MHGSPARS.TXT | short ASCII formatted Info |
FILE_ID.DIZ | dto. |
PAD_FILE.XML | Portable Application Description file for software portals |
README.HTM | this file |
LOGOMHGS.GIF | our logo |
FAVICON.ICO | TFunctionParser Icon |
INSTALL.BAT | installs TFunctionParser (only Win2k/XP/2003/Vista/2008/7/8.x), see below |
UNINSTAL.BAT | uninstalls TFunctionParser (only Win2k/XP/2003/Vista/2008/7/8.x), see below |
FDEMO.DPR FDEMO1.PAS FDEMO1.DFM | providing a simple demo application for TFunctionParser (function plotter) under Delphi (subfolder "Delphi") |
CALC.VBP CALC.VBW FORM1.FRM FORM1.FRX | providing a simple demo application of TFunctionParser for Visual Basic (subfolder "VisualBasic") |
CALC.HTA | simple demo as Hypertext Application with JavaScript (subfolder "Scripting") |
CALC.VBS | sample script for using Windows Script Host with VBScript (subfolder "Scripting") |
CONST.PL | sample script for Perl (subfolder "Scripting") |
CALC.DOC | sample macro for VBA under MS Word (subfolder "Office") |
CALC.XLS | sample macro for VBA under MS Excel (subfolder "Office") |
CS_NET.ZIP | Zip archive containing a .NET sample written in C# |
Examples for MSVC++ are available directly from us.
The essential file is MHGSPARS.DLL that contains TFunctionParser. It must be copied to your harddisk.Windows requires the registration of COM/Automation-Servers in the Registry (which will require administrator privileges.).
This registration process can be done in different ways:
regsvr32 <PATH>\MHGSPARS.DLL
<PATH>
is the path of MHGSPARS.DLL.regsvr32 -u <PATH>\MHGSPARS.DLL
-s
suppresses the display of messageboxes.function DllRegisterServer: HResult; stdcall;
function DllUnregisterServer: HResult; stdcall;
Get a handle to the DLL with LoadLibrary, then call GetProcAddress for obtaining the procedure entry point:
var dllinst: THandle; //Handle of DLL DllRegisterServer: function:HRESULT; //function address installdir:string; //path of installation folder . . dllinst:=LoadLibrary(pchar(installdir+'\mhgspars.dll')); if dllinst=0 then ... //Errorhandling @DllRegisterServer:=GetProcAddress(dllinst,'DllRegisterServer'); DllRegisterServer; . FreeLibrary(dllinst); |
If you want to install the TFunctionParser 32 bit variant on a 64 bit operating system (to use it with 32 bit clients), you will need to install it to the 32 bit subsystem (install with e.g. c:\Windows\SysWOW64\regsvr32.exe).
AngleMode | ErrorMessage | Term |
CleanUp | FunctionsText | Result |
ConstantsText | GetVarValue | VarNamesText |
DefineUserFunction | SetVarValue | Version |
ErrorCode | ShowInfo |
Term is a character string (e.g. 'sin(x)') defining the given function.
Spaces are
ignored. Lower or upper cases are not distinguished.
Arguments of functions must
be in brackets.
Multiple arguments in functions must be separated by commata or semicola.
Only relevant for trigonometric functions and their inverses (arc functions). If TFunctionParser.Anglemode is RAD the unit for angles is radians (full angle =2*pi), if it is DEG the unit is degrees (full angle =360°), and if it is GON the unit is gons (full angle =400 gons).
You can define variable names with VarNamesText. Default are x, y, z and t.
Each variable name must stand in a separat line (lines must be separated by CR LF (0xD,0xA)).
You may make comments in the same lines, after a '#'-sign (e.g. 'x #that is x');
If you use multiple TFunctionParser instances they will use the same variable names.
You can define an arbitrary number of constants with ConstantsText
using the notation e.g. 'A1=1.5e-3'.
For TFunctionParser it is even possible to define constants like 'A2=sin(pi/4)' or 'A3=A2-A1'
Warning: The call of variable names (e.g. 'A4=exp(y)') will lead to wrong results.
You must not call constants recursively (e.g. 'A5=sqr(A5+1)') or crosswise
(e.g. 'A6=A7', 'A7=A6').
The constant identifiers must be unique. Otherwise the first one in the list is
taken.
Reserved abbreviations for identifier are e, pi, C, TRUE and FALSE.
The sequence of the definition of the constants doesn't matter. You may even add
your own comments in separat lines.
Lines must be separated by CR LF (0xD,0xA).
ConstantsText is automatically initialized with a list of physical constants. Read the property ConstantsText for obtaining the list. (cf. sample CONST.PL).
If you use multiple TFunctionParser instances they will use the same defined constants.
In this list you can define functions (macros) that are based on the intrinsic
functions. For example: 'F(X;Y)=EXP(X)*COS(Y)'.
You must specify the arguments in brackets and separated by semicola on the left side.
There must be at least one argument.
The programmer is responsible for correct syntax and unique identifiers.
Userdefined functions may depend on other userdefined funclions, like e.g.
'G(X)=SQR(F(X;PI))
Recursive or crosswise calls are not allowed.
Warning: If you use identifiers of predefined functions (e.g. SIN(X)) the latter
ones are overwritten.
Your own comments may be included after a '#'-sign.
Lines must be separated by CR LF (0xD,0xA).
If you use multiple TFunctionParser instances they will use the same defined functions.
See CALC.VBS for a sample how to use the FunctionText property.
When you read the Result, the Term is evaluated and the function value returned.
If an error occurs the ErrorCode is set according to the following list:
0 | everything is OK |
1 | wrong parentheses / separators |
2 | Syntax error / invalid term |
3 | unknown identifier |
4 | recursive function or constant |
5 | wrong number of operands |
6 | term is not analyzed |
7 | function is not defined for argument(s) |
8 | NaN: evaluation aborted |
9 | declarations not assigned |
You should check ErrorCode after assigning the term and after reading the result.
The Errormessage gives you detailed verbose information on the errors that occured.
Version indicates the used version.
You can define the variable value of any variable defined in the VarNamesText property.
the name of the variable.
Valuethe numerical value of the variable.
GetVarValue returns the value of any variable that is defined in the VarNamesText property.
the name of the variable.
Valuethe numerical value of the variable.
DefineUserFunction allows the definition of functions, which were written by yourself.
This feature is not available for scripting and programming languages which don't support function pointers.
the name of the function.
Valuethe address of the function. The address of the function is given as an unsigned longword (32bit), which can be retrieved as the value of the function pointer. Please see remarks section below.
DefineUserFunction returns TRUE if the function definition was accepted, otherwise FALSE.
DefineUserFunction allows you to call a function whose code you wrote yourself.
Your function must be declared as
function myfunction(X:double):double.
Then you can call
DefineUserFunction('MyFunctionName',DWORD(@myfunction)).
Your function is accessed if you call 'MyFunctionName(x)' in the Term.
You can define multiple functions that way.
For undefining a previously defined function simply call
DefineUserFunction('MyFunctionName',nil).
ShowInfo displays the about screen.
If you do not need TFunctionParser anymore and don't want to rely on garbage collection you can call CleanUp to free used resources. Warning: If you use TFunctionParser afterwards this can lead to an access violation.
The first character must be alphabetical. The remaining characters may be alphabetic or numerical. Special symbols are not supported (except underscore). Identifiers must be unique.
For working with TFunctionParser you should perform the following steps:
Necessary steps are indicated by consecutive numbers. Letters mark optional steps that may
be omitted if you don't want to use the respective features.
Please note that TFunctionParser must be installed on the respective computer.
(*1*) | Create a TFunctionParser Object. The HowTo depends on your automation client. See the respective reference for more information. |
(*A*) | Define Variables using the VarNamesText property (optional). |
(*B*) | Define Constants using the ConstantsText property (optional). |
(*C*) | Define Functions using the FunctionsText property (optional). |
(*D*) | Define own functions with DefineUserFunction (optional, client must support function pointers). |
(*E*) | Change the AngleMode using the AngleMode property (optional). |
(*2*) | Set the Term property to the desired function term. |
(*F*) | Check the ErrorCode or ErrorMessage (optional). |
(*3*) | Set all required variable values using SetVarValue. If you don't use variables you can omit this step. |
(*4*) | Read the Result. |
(*G*) | Check the ErrorCode or ErrorMessage (optional). |
Attached to the MHGSPARS.ZIP file there are multiple sample scripts or programs.
Some of them are quite easy. They are only intented to demonstrate the general access on TFunctionParser in the respective language. If you have sufficient knowledge in the respective client language you should have no problems to modify and expand the listings for suiting your own wishes.
Some samples are a little more complex to show the capabilities of TFunctionParser.
The following Delphi listing shows the general use of TFunctionParser.
A simple calculator program may contain the following code.
Clicking on a button will induce the program to read the function term and the variable
value from the respective edit fields, evaluate these, and write the result as the
caption of a label.
... uses ComObj; var FunctionParser:VARIANT; procedure TForm1.FormCreate(Sender: TObject); begin FunctionParser:=CreateOleObject('mhgspars.FunctionParser'); //(*1*) end; procedure TForm1.BitBtn1Click(Sender: TObject); const xn:string='x'; begin FunctionParser.Term:=edit1.text; //(*2*) {e.g. Term:='sin(x)/x'} FunctionParser.SetVarValue(xn,strtofloat(edit2.text)); //(*3*) {'x' must be defined as variable name} label1.caption:='Result = '+floattostr(FunctionParser.Result); //(*4*) if FunctionParser.ErrorCode<>0 then label1.caption:=FunctionParser.ErrorMessage; //(*F*) {It is indeed that easy to program a calculator} end; ... |
The separator between multiple arguments in functions is the semicolon (;).
The decimal separator is the period (.).
Depending on your regional settings, the comma (,) may be used either as decimal or list separator.
The fourth column specifies the availability for TFunctionParser.