by Leonid Sakharov

Calculator of math expression.
There is a demonstration of the algorithm for calculation of the string with mathematical expression. There are virtually identical C++ and PHP class implementations of the algorithm. At this page the work of PHP code can be directly tested. C++ codes are implemented in LeoCalculator and LeoDataAnalysis software applications.

The class can be used for giving user a flexibility in defining input parameter as mathematical formulas that can be very helpful in many scientific presentations or modeling algorthms.

The syntax of the mathematical expression to be calculated is mostly standard one:

Numbers can be presented as integers like 11 or float like 3.2e-4 or -2.8E2 with or without plus or minus signs before them as logically fit.

Arithmetic operations + - * /  are used for basic : addition, subtraction, multiplication and division correspondently.

Sign ^ performs raising base to the power exponent like 3^5=243 when it can be done  (the exclusion is when base is negative and power is not integer).

Brackets ( and ) are used to priorities operations.

It is possible to use in expression variables defined as numbers. There are two predefined variables: pi = 3.14159265359 and e = 2.7182818284590452353602874.

The following functions are currently supported:

Logarithm and exponential functions:

log(x,y) - where x - the number, y - base;

log10(x) - logarithm when base is 10;

ln(x) - natural logarithm when base is Euler number e= 2.7182818284590452353602874.

pow(x,y)  -  raising base (x) to the power (y).

exp(x) - raising base equal Euler number into power x.


abs(x) - positive of x; abs(-x)=x

int(x) - take integer from float x for example: int(1.3) = 1, int (-2.4) = -2.

floor(x) - integer of float x that is smallest of neighbors. floor(-2.4) = -3

!(x) - factorial of x. In this implementation x can be not only integer but definitely only positive.

if(x1,x2,x3,x4) - results of the function is conditional to the first argument. If x1<0 function return x2; if x1=0 the function return x3; if x1>0 function return x4. The function can be used as delta function for example - if(3,0,1,0) will be eqvivalent to delta function of 3.

Random numbers generators:

rand() - result is random number in interval from 0 to 1.0;

gauss(x) - result is random number in interval from -∞ to ∞ with maximum of probability distribution at 0 and standard deviation equal x.

Trigonometric functions:

sin(x) - sine of x.

cos(x) - cosine of x.

tan(x) -  tangent of x;

Inverse trigonometric functions:

asin(x) - invert sine of x

acos(x) - invert cosine of x.

atan(x) - invert tangent of x.

Argument x in trigonometric functions is expressed in radian. The same a result of invert trigonometric functions is in radian.

rad(x) - convert x expressed in degree into radian;

degree(x) - convert x expressed in radian into degree;

Statistical and related functions:

sum(x1,x2,x3...xN) - sum of all arguments. There are no specific limits imposed on number of arguments.

mean(x1,x2,x3...xN) - average of arguments.

sigma(x1,x2,x3...xN) - standard deviation of arguments.

student(x1,x2,x3) - where x1 - standard deviation, x2 - number points in series, x3 - defines trust interval from -x3 to x3 presuming that average is 0. The function returns probability to be inside trust interval.

trust(x1,x2,x3) - where x1 - standard deviation, x2 - number points in series, x3 - a probability to be inside trust interval. The function returns value t that defines trust interval as from -t to t assuming that average is zerro.

Solution of equation:

solve1(x1,x2) - produce solution of linear equation x1*x+x2 =0.

solve2p(x1,x2,x3) produce largest of two solutions of two solutions of square equation: x1*x*x+x2*x+x3=0.

solve2m(x1,x2,x3) produce smallest of two solutions of two solutions of square equation: x1*x*x+x2*x+x3=0

Jun. 1, 2017; 09:46 EST

About Products Data analysis Crystal growth E-Vault Downloads Donate Contact Site Map © LeoKrut