Simple expressions

FOR COMMERCIAL USE, PLEASE ENSURE YOU HAVE A COMMERCIAL LICENSE.
ANOTHER WAY TO SUPPORT MY WORK IS BY PURCHASING MY BOOK, CUSTOMER FIRST, VALUE NEXT.
THANK YOU FOR SUPPORTING MY WORK!

The tutorial consists of more than 200 live examples from 50 sections given separately for JAVA, C# and C++. Each of the examples can be copied and run on your own environment. In addition, mXparser provides an extensive collection of over 500 built-in math functions, expressions and symbols. Familiarize yourself with the scope and the syntax. Live testing is the best way to learn. Good luck! 🙂

Tutorial Math Collection API spec Download

Below is the code for JAVA, C# (the code for C# is almost identical) and C++. To copy the code, double-click inside the frame.

You may also be interested in the following tutorial sections:

Case 1: Simple calculation

$$2+1$$

Java/C# code
// JAVA: import org.mariuszgromada.math.mxparser.*;
// C#: using org.mariuszgromada.math.mxparser;
// ...
Expression e = new Expression("2+1");
mXparser.consolePrintln("Res: " + e.getExpressionString() + " = " + e.calculate());
C++ code
#include "org/mariuszgromada/math/mxparser.hpp"
// ...
ExpressionPtr e = new_Expression("2+1");
mXparser::consolePrintln("Res: " + e->getExpressionString() + " = " + e->calculate());
Code result
[mXparser-v.5.2.1] Res: 2+1 = 3.0

Case 2: Changing expression string

$$2-1$$

Java/C# code
// JAVA: import org.mariuszgromada.math.mxparser.*;
// C#: using org.mariuszgromada.math.mxparser;
// ...
Expression e = new Expression("2+1");
e.setExpressionString("2-1");
mXparser.consolePrintln("Res: " + e.getExpressionString() + " = " + e.calculate());
C++ code
#include "org/mariuszgromada/math/mxparser.hpp"
// ...
ExpressionPtr e = new_Expression("2+1");
e->setExpressionString("2-1");
mXparser::consolePrintln("Res: " + e->getExpressionString() + " = " + e->calculate());
Code result
[mXparser-v.5.2.1] Res: 2-1 = 1.0

Case 3: Using operators

$$2-\frac{32-4}{23+\frac{4}{5}}-(2-4)(4+6-98.2)+4$$

Java/C# code
// JAVA: import org.mariuszgromada.math.mxparser.*;
// C#: using org.mariuszgromada.math.mxparser;
// ...
Expression e = new Expression("2-(32-4)/(23+4/5)-(2-4)*(4+6-98.2)+4");
mXparser.consolePrintln("Res: " + e.getExpressionString() + " = " + e.calculate());
C++ code
#include "org/mariuszgromada/math/mxparser.hpp"
// ...
ExpressionPtr e = new_Expression("2-(32-4)/(23+4/5)-(2-4)*(4+6-98.2)+4");
mXparser::consolePrintln("Res: " + e->getExpressionString() + " = " + e->calculate());
Code result
[mXparser-v.5.2.1] Res: 2-(32-4)/(23+4/5)-(2-4)*(4+6-98.2)+4 = -171.57647058823528

Case 4: Power function

$$2^3+2^{3}+2^{3^{-4}}$$

Java/C# code
// JAVA: import org.mariuszgromada.math.mxparser.*;
// C#: using org.mariuszgromada.math.mxparser;
// ...
Expression e = new Expression("2^3+2^(-3)+2^3^(-4)");
mXparser.consolePrintln("Res: " + e.getExpressionString() + " = " + e.calculate());
C++ code
#include "org/mariuszgromada/math/mxparser.hpp"
// ...
ExpressionPtr e = new_Expression("2^3+2^(-3)+2^3^(-4)");
mXparser::consolePrintln("Res: " + e->getExpressionString() + " = " + e->calculate());
Code result
[mXparser-v.5.2.1] Res: 2^3+2^(-3)+2^3^(-4) = 9.133594091576999

Case 5: Using numbers in scientific notation

Java/C# code
// JAVA: import org.mariuszgromada.math.mxparser.*;
// C#: using org.mariuszgromada.math.mxparser;
// ...
Expression e1 = new Expression("1.2e2 + 1.2e+2 + 1.2e-2");
mXparser.consolePrintln("Res 1: " + e1.getExpressionString() + " = " + e1.calculate() );
Expression e2 = new Expression("1.2E2 + 1.2E+2 + 1.2E-2");
mXparser.consolePrintln("Res 2: " + e2.getExpressionString() + " = " + e2.calculate() );
C++ code
#include "org/mariuszgromada/math/mxparser.hpp"
// ...
ExpressionPtr e1 = new_Expression("1.2e2 + 1.2e+2 + 1.2e-2");
mXparser::consolePrintln("Res 1: " + e1->getExpressionString() + " = " + e1->calculate() );
ExpressionPtr e2 = new_Expression("1.2E2 + 1.2E+2 + 1.2E-2");
mXparser::consolePrintln("Res 2: " + e2->getExpressionString() + " = " + e2->calculate() );
Code result
[mXparser-v.5.2.1] Res 1: 1.2e2 + 1.2e+2 + 1.2e-2 = 240.012
[mXparser-v.5.2.1] Res 2: 1.2E2 + 1.2E+2 + 1.2E-2 = 240.012

Case 6: Percent sign support

Java/C# code
// JAVA: import org.mariuszgromada.math.mxparser.*;
// C#: using org.mariuszgromada.math.mxparser;
// ...
Expression e1 = new Expression("2%");
Expression e2 = new Expression("2% * 100");
Expression e3 = new Expression("pi% * 100");
mXparser.consolePrintln("Res 1: " + e1.getExpressionString() + " = " + e1.calculate());
mXparser.consolePrintln("Res 2: " + e2.getExpressionString() + " = " + e2.calculate());
mXparser.consolePrintln("Res 3: " + e3.getExpressionString() + " = " + e3.calculate());
C++ code
#include "org/mariuszgromada/math/mxparser.hpp"
// ...
ExpressionPtr e1 = new_Expression("2%");
ExpressionPtr e2 = new_Expression("2% * 100");
ExpressionPtr e3 = new_Expression("pi% * 100");
mXparser::consolePrintln("Res 1: " + e1->getExpressionString() + " = " + e1->calculate());
mXparser::consolePrintln("Res 2: " + e2->getExpressionString() + " = " + e2->calculate());
mXparser::consolePrintln("Res 3: " + e3->getExpressionString() + " = " + e3->calculate());
Code result
[mXparser-v.5.2.1] Res 1: 2% = 0.02
[mXparser-v.5.2.1] Res 2: 2% * 100 = 2.0
[mXparser-v.5.2.1] Res 3: pi% * 100 = 3.141592653589793

Case 7: Leading zeros support

Java/C# code
// JAVA: import org.mariuszgromada.math.mxparser.*;
// C#: using org.mariuszgromada.math.mxparser;
// ...
Expression e1 = new Expression("00123");
Expression e2 = new Expression("-00123");
Expression e3 = new Expression("-00000123.123e-10");
mXparser.consolePrintln("Res 1: " + e1.getExpressionString() + " = " + e1.calculate());
mXparser.consolePrintln("Res 2: " + e2.getExpressionString() + " = " + e2.calculate());
mXparser.consolePrintln("Res 3: " + e3.getExpressionString() + " = " + e3.calculate());
C++ code
#include "org/mariuszgromada/math/mxparser.hpp"
// ...
ExpressionPtr e1 = new_Expression("00123");
ExpressionPtr e2 = new_Expression("-00123");
ExpressionPtr e3 = new_Expression("-00000123.123e-10");
mXparser::consolePrintln("Res 1: " + e1->getExpressionString() + " = " + e1->calculate());
mXparser::consolePrintln("Res 2: " + e2->getExpressionString() + " = " + e2->calculate());
mXparser::consolePrintln("Res 3: " + e3->getExpressionString() + " = " + e3->calculate());
Code result
[mXparser-v.5.2.1] Res 1: 00123 = 123.0
[mXparser-v.5.2.1] Res 2: -00123 = -123.0
[mXparser-v.5.2.1] Res 3: -00000123.123e-10 = -1.23123E-8

Case 8: Fractions support

Java/C# code
// JAVA: import org.mariuszgromada.math.mxparser.*;
// C#: using org.mariuszgromada.math.mxparser;
// ...
Expression e = new Expression("1_2 + 2_1_2");
mXparser.consolePrintln("Res: " + e.getExpressionString() + " = " + e.calculate());
C++ code
#include "org/mariuszgromada/math/mxparser.hpp"
// ...
ExpressionPtr e = new_Expression("1_2 + 2_1_2");
mXparser::consolePrintln("Res: " + e->getExpressionString() + " = " + e->calculate());
Code result
[mXparser-v.5.2.1] Res: 1_2 + 2_1_2 = 3.0

Case 9: New division operators (since v.6.0)

Java/C# code
// Central & Eastern Europe ":"
Expression e1 = new Expression(("6:3"));
// Integer division (quotient) "\"
Expression e2 = new Expression(("7\\3"));

// Results
mXparser.consolePrintln(e1.getExpressionString() + " = " + e1.calculate() + " // Central & Eastern Europe division");
mXparser.consolePrintln(e2.getExpressionString() + " = " + e2.calculate() + " // Integer division (quotient)");
C++ code
#include "org/mariuszgromada/math/mxparser.hpp"
// ...
// Central & Eastern Europe ":"
ExpressionPtr e1 = new_Expression(("6:3"));
// Integer division (quotient) "\"
ExpressionPtr e2 = new_Expression(("7\\3"));

// Results
mXparser::consolePrintln(e1->getExpressionString() + " = " + e1->calculate() + " // Central & Eastern Europe division");
mXparser::consolePrintln(e2->getExpressionString() + " = " + e2->calculate() + " // Integer division (quotient)");
Code result
[mXparser-v.6.0.0] 6:3 = 2.0 // Central & Eastern Europe division
[mXparser-v.6.0.0] 7\3 = 2.0 // Integer division (quotient)
Nuget – Package Manager (C#, F#, Visual Basic, …)

Install-Package MathParser.org-mXparser-Version 6.1.1

Nuget – .NET CLI

dotnet add package MathParser.org-mXparser--version 6.1.1

Nuget – Package Reference

<PackageReference Include="MathParser.org-mXparser"Version="6.1.1"/>

Maven – Dependency (Java, Kotlin, Scala, Groovy, …)

<dependency>
<groupid>
org.mariuszgromada.math</groupid>
<artifactid>
MathParser.org-mXparser</artifactid>
<version>
6.1.1</version>
</dependency>

Maven – Gradle

implementation 'org.mariuszgromada.math:MathParser.org-mXparser:6.1.1'

CMake – Dependency / FetchContent (C++, MSVC, LLVM/Clang, GNU/GCC, MinGW, MSYS2, WSL, Windows, Linux, Unix, MacOS)

include(FetchContent)
FetchContent_Declare(
MathParserOrgMxParser
GIT_REPOSITORY
https://github.com/mariuszgromada/MathParser.org-mXparser.git
GIT_TAG
v.6.1.1
SOURCE_SUBDIR CURRENT/cpp/lib
)
FetchContent_MakeAvailable(
MathParserOrgMxParser)
mxparser_link(YourExecutable)

GitHub

git clone https://github.com/mariuszgromada/MathParser.org-mXparser

OTHER DOWNLOAD OPTIONS

Download latest release – v.6.1.0 Sagitara: .NET bin onlyDownload latest release – v.6.1.0 Sagitara: JAVA bin onlyDownload latest release – v.6.1.0 Sagitara: bin + doc

NEWS FROM MATHPARSER.ORG
SOURCE CODE

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

My other creative spaces

DONATION
Did you find the software useful?
Please consider donation 🙂
DONATE