About me
 Favourite links
 Cities i visited
 Русская версия


  Mail me

MathParser Reference

About | Download | Reference

(The most of this reference is taken from the reference to CCalc library)


MathParser formulas syntax

  You may use mathematically-correct formulas, including brackets, functions etc.

MathParser formulas syntax admits:

  • Mathematical constants E and Pi
  • Large number of arithmetical and bitwise operations:
    + - * /
    It is obvious
    & | ^ ~
    Bitwise and / or / xor / not
    Without name. See notes.
  • Logical operations and relations
    <> = > <
    not equal / equal / greater / less relations
    && ||
    Logical and / or
  • Functions
  • Conditional operator
    <condition> ? <true expr.> : <false expr.>
    - logical condition. 0 - false, nonzero - true. Logical relations and operations may be used.
    <true expr.>
    - expression for true expression
    <false expr.>
    - expression for false expression

Correct formulas examples

(sin(Pi/2) + 1) / 4
= 0.5
2 > 3
= 0 (false)
(2*2=4) ? 1 : 0
= 1
round( 1.2345, 2 )
= 1.23
round( 27.45, -1 )
= 30


$ operation.
A $ B - integerly divide A by B; if there was a remainder, the result is increased by 1. Usage example: for production B (or less) units of wares we spend one unit of resource ("discrete" resource). It is necessary to know how much resource units we need for producting A units of warez.

MathParser script

MathParser script begins with "#!". The syntax is clear from the example:

a := 2;
b := 3;
return a * b + 1;

CCalc Library

There library consists of three Classes with their own API:

CStrMap (StrMap.h)
Small "sublibrary" for working with "string lists". You must use this class if you want to add your own user-defined functions (UDFs) and user-defined variables (UDVs)
CLexer (lexer.h)
Universal lexical analyser used as backend layer for parsing expressions. In general developers have no need to communicate directly with Lexer. But they can use it separately for their own needs.
MathParser (MathParser.h)
Core class which calculates expressions. It is very easy to use but may be hard to initialize to match your needs.

StrMap reference

class CStrMap{
	int FCount, FCapacity;
	int FExtraLen, FRecordLen;
	nt FDoDuplicate;
	char *FList;
	CStrMap( int extrabytes=sizeof(double), int dup=0);
	void CreateFromChain( int extrabytes, char *strchain, void *data );
	void AddString(char *str, void *data);
	void AddStrLen(char *str, int len, void *data );
	void ShrinkMem();
	void Trim(int NewCount );
	void TrimClear(int NewCount );
	void SetCapacity(int NewCapacity );
	int IndexOf(char *str, void **data );
	int LenIndexOf(char *str, int len, void **data );
	char* GetString(int index, int *len, void **data );
	void FillFromChain(char *strchain, void *data );

(Note: from library version you don't have to create parameters CStrMap, CMathParser::Parameters now is not pointer but a "normal" object)
I have no time to describe each function, just some examples of most common usage:

Creating list of variables from "char chain"

Variables will be associated with double values:

char varnames[] = "X\000" "Y\000" "Z\000\000";
double varvalues[] = { 5.3, 6.1, -7.45 };

mp.Parameters.FillFromChain( sizeof(double), varnames, varvalues );


Note that varnames is a special data structure ("char chain"):
a string containing zero-separated "substrings" terminated by double zero.

Creating list of variables step-by-step

double varvalues[] = { 5.3, 6.1 };

mp.Parameters.AddString( "X", varvalues );
mp.Parameters.AddString( "Y", varvalues+1 );

MathParser reference

class CMathParser{
	Operation OpStack[MAX_STACK_SIZE];
	double	ValStack[MAX_STACK_SIZE];
	int	OpTop, ValTop;
	int	ObrDist;
	CLexer Lexer;
	int	script;
	CStrMap *VarParams;
	char* ParseScript(double *result);
	char* ParseFormula(double *result);
	char* PrepareFormula();
	char* Calc();
	char* CalcToObr();
	CStrMap *Parameters;
	CStrMap *ExtFunctions;
	PrmSrchFunc MoreParams;
	void  *ParamFuncParam;
	CMathParser( char *MoreLetters=NULL );
	char* Parse(char *Formula, double *result);
Some examples of usage:

Initializing MathParser for using with list of UDVs

CMathParser* parser = new CMathParser();

/* ... creating list of variables. See CStrMap reference for example */

parser.Parameters.AddString("X", &x);

/* ... parsing expressions */

delete parser;

Calculating expression

double result;
char *ErrMsg = parser->Parse("sin(x * x) / 2", &result );
if ( ErrMsg == NULL )
    printf( "%.15G\n", result );
else {
    puts( ErrMsg );

Of course this documentation is incomplete as I have no time now to write more. For more information see test.cpp as an example of usage of the library, or you can contact me by e-mail (zkv@mail.ru)

 © 2002 Kirill Zaborski
Сайт создан в системе uCoz