Using built-in functions

MathParser.org - mXparser - logo - Tutorial

*** If you found the software useful donation is something you might consider 🙂 ***

If you found the software useful donation is something you might consider :-)

JAVA code

Case 1: Unary function

import org.mariuszgromada.math.mxparser.*;
...
Expression e = new Expression("sin(2)-cos(3)");
mXparser.consolePrintln("Res: " + e.getExpressionString() + " = " + e.calculate());
Res: sin(2)-cos(3) = 1.8992899234261271

Case 2: Binary function #1

import org.mariuszgromada.math.mxparser.*;
...
Expression e = new Expression("min(3,4) + max(-2,-1)");
mXparser.consolePrintln("Res: " + e.getExpressionString() + " = " + e.calculate());
Res: min(3,4)+max(-2,-1) = 2.0

Case 3: Binary function #2

import org.mariuszgromada.math.mxparser.*;
...
Expression e = new Expression("C(10,5)");
mXparser.consolePrintln("Res: " + e.getExpressionString() + " = " + e.calculate());
Res: C(10,5) = 252.0

Case 4: Function with 3 arguments #1 - condition checking

import org.mariuszgromada.math.mxparser.*;
...
Expression e = new Expression("if(2<3, 1, 0)");
mXparser.consolePrintln("Res: " + e.getExpressionString() + " = " + e.calculate());
Res: if(2<3, 1, 0) = 1.0

Case 5: Function with 3 arguments #2

import org.mariuszgromada.math.mxparser.*;
...
Expression e = new Expression("if(3<2, 1, if(1=1, 5, 0) )");
mXparser.consolePrintln("Res: " + e.getExpressionString() + " = " + e.calculate());
Res: if(3<2, 1, if(1=1, 5, 0) ) = 5.0

Case 6: Function with n-arguments

import org.mariuszgromada.math.mxparser.*;
...
Expression e = new Expression("gcd(4,2,8,20)");
mXparser.consolePrintln("Res: " + e.getExpressionString() + " = " + e.calculate());
Res: gcd(4,2,8,20) = 2.0

Case 7: Function with even number of arguments

import org.mariuszgromada.math.mxparser.*;
...
Expression e = new Expression("iff( 1>2, 1, 3<2, 2, 5>3, 3, 7 < 3, 4 )");
mXparser.consolePrintln("Res: " + e.getExpressionString() + " = " + e.calculate());
Res: iff( 1>2, 1, 3<2, 2, 5>3, 3, 7 < 3, 4 ) = 3.0

Case 8: Equivalent to case 7 - more convienient form of marking parameters

import org.mariuszgromada.math.mxparser.*;
...
Expression e = new Expression("iff( 1>2, 1; 3<2, 2; 5>3, 3; 7 < 3, 4 )");
mXparser.consolePrintln("Res: " + e.getExpressionString() + " = " + e.calculate());
Res: iff( 1>2, 1; 3<2, 2; 5>3, 3; 7 < 3, 4 ) = 3.0

*** If you found the software useful donation is something you might consider 🙂 ***

If you found the software useful donation is something you might consider :-)

Enjoy! 🙂

 

Best regards,

Mariusz Gromada

Download latest release – v.4.2.0 Aquaria: bin + doc + src (.zip 15.5 MB)

Download latest release – v.4.2.0 Aquaria: bin only (.zip 5.99 MB) – separate binaries for java: 1.6, 1.7, 1.8; .NET: 2.0, 3.0, 3.5, 4.0, 4.5, 4.6, 4.7, .NET Core, .NET Standard, .NET PCL, Xamarin.Android, Xamarin.iOS

Source code .zipSource code .tar.gz View on GitHubMathSpace.pl