“if” and “iff” functions

TO SUPPORT MY WORK, ORDER A COMMERCIAL LICENSE
THANK YOU!

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.

Case 1: Mechanics of the if function

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

Case 2: “if” function and arguments

Java/C# code
// JAVA: import org.mariuszgromada.math.mxparser.*;
// C#: using org.mariuszgromada.math.mxparser;
// ...
Argument x = new Argument("x = 2");
Expression e = new Expression("if(x<3, x+2, x-2)", x);
mXparser.consolePrintln("Res 1: " + " x = " + x.getArgumentValue() + ", " + e.getExpressionString() + " = " + e.calculate());
x.setArgumentValue(4);
mXparser.consolePrintln("Res 2: " + " x = " + x.getArgumentValue() + ", " + e.getExpressionString() + " = " + e.calculate());
C++ code
#include "org/mariuszgromada/math/mxparser.hpp"
// ...
ArgumentPtr x = new_Argument("x = 2");
ExpressionPtr e = new_Expression("if(x<3, x+2, x-2)", x);
mXparser_consolePrintln("Res 1: " + " x = " + x->getArgumentValue() + ", " + e->getExpressionString() + " = " + e->calculate());
x->setArgumentValue(4);
mXparser_consolePrintln("Res 2: " + " x = " + x->getArgumentValue() + ", " + e->getExpressionString() + " = " + e->calculate());
Code result
[mXparser-v.5.2.1] Res 1:  x = 2.0, if(x<3, x+2, x-2) = 4.0
[mXparser-v.5.2.1] Res 2:  x = 4.0, if(x<3, x+2, x-2) = 2.0

Case 3: “if” function in user defined function

Java/C# code
// JAVA: import org.mariuszgromada.math.mxparser.*;
// C#: using org.mariuszgromada.math.mxparser;
// ...
Function f = new Function("f(x) = if(x<3, x+2, x-2)");
Expression e1 = new Expression("f(2)", f);
Expression e2 = new Expression("f(4)", f);
mXparser.consolePrintln("Res 1: " + e1.getExpressionString() + " = " + e1.calculate());
mXparser.consolePrintln("Res 2: " + e2.getExpressionString() + " = " + e2.calculate());
C++ code
#include "org/mariuszgromada/math/mxparser.hpp"
// ...
FunctionPtr f = new_Function("f(x) = if(x<3, x+2, x-2)");
ExpressionPtr e1 = new_Expression("f(2)", f);
ExpressionPtr e2 = new_Expression("f(4)", f);
mXparser::consolePrintln("Res 1: " + e1->getExpressionString() + " = " + e1->calculate());
mXparser::consolePrintln("Res 2: " + e2->getExpressionString() + " = " + e2->calculate());
Code result
[mXparser-v.5.2.1] Res 1: f(2) = 4.0
[mXparser-v.5.2.1] Res 2: f(4) = 2.0

Case 4: Mechanics of the “iff” function

Java/C# code
// JAVA: import org.mariuszgromada.math.mxparser.*;
// C#: using org.mariuszgromada.math.mxparser;
// ...
Argument x = new Argument("x = 0.5");
Expression e = new Expression("iff(x<1, 1; x<2, 2; x<3, 3)", x);
mXparser.consolePrintln("Res 1: " + " x = " + x.getArgumentValue() + ", " + e.getExpressionString() + " = " + e.calculate());
x.setArgumentValue(1.5);
mXparser.consolePrintln("Res 2: " + " x = " + x.getArgumentValue() + ", " + e.getExpressionString() + " = " + e.calculate());
x.setArgumentValue(2.5);
mXparser.consolePrintln("Res 3: " + " x = " + x.getArgumentValue() + ", " + e.getExpressionString() + " = " + e.calculate());
x.setArgumentValue(3.5);
mXparser.consolePrintln("Res 4: " + " x = " + x.getArgumentValue() + ", " + e.getExpressionString() + " = " + e.calculate());
C++ code
#include "org/mariuszgromada/math/mxparser.hpp"
// ...
ArgumentPtr x = new_Argument("x = 0.5");
ExpressionPtr e = new_Expression("iff(x<1, 1; x<2, 2; x<3, 3)", x);
mXparser_consolePrintln("Res 1: " + " x = " + x->getArgumentValue() + ", " + e->getExpressionString() + " = " + e->calculate());
x->setArgumentValue(1.5);
mXparser_consolePrintln("Res 2: " + " x = " + x->getArgumentValue() + ", " + e->getExpressionString() + " = " + e->calculate());
x->setArgumentValue(2.5);
mXparser_consolePrintln("Res 3: " + " x = " + x->getArgumentValue() + ", " + e->getExpressionString() + " = " + e->calculate());
x->setArgumentValue(3.5);
mXparser_consolePrintln("Res 4: " + " x = " + x->getArgumentValue() + ", " + e->getExpressionString() + " = " + e->calculate());
Code result
[mXparser-v.5.2.1] Res 1:  x = 0.5, iff(x<1, 1; x<2, 2; x<3, 3) = 1.0
[mXparser-v.5.2.1] Res 2:  x = 1.5, iff(x<1, 1; x<2, 2; x<3, 3) = 2.0
[mXparser-v.5.2.1] Res 3:  x = 2.5, iff(x<1, 1; x<2, 2; x<3, 3) = 3.0
[mXparser-v.5.2.1] Res 4:  x = 3.5, iff(x<1, 1; x<2, 2; x<3, 3) = NaN

Case 5: “iff” function is not limited in number of cases

Java/C# code
// JAVA: import org.mariuszgromada.math.mxparser.*;
// C#: using org.mariuszgromada.math.mxparser;
// ...
Argument x = new Argument("x = 0.5");
Expression e = new Expression("iff(x<1, 1; x<2, 2; x<3, 3; x<4, 4; x<5, 5)", x);
mXparser.consolePrintln("Res 1: " + " x = " + x.getArgumentValue() + ", " + e.getExpressionString() + " = " + e.calculate());
x.setArgumentValue(1.5);
mXparser.consolePrintln("Res 2: " + " x = " + x.getArgumentValue() + ", " + e.getExpressionString() + " = " + e.calculate());
x.setArgumentValue(2.5);
mXparser.consolePrintln("Res 3: " + " x = " + x.getArgumentValue() + ", " + e.getExpressionString() + " = " + e.calculate());
x.setArgumentValue(3.5);
mXparser.consolePrintln("Res 4: " + " x = " + x.getArgumentValue() + ", " + e.getExpressionString() + " = " + e.calculate());
x.setArgumentValue(4.5);
mXparser.consolePrintln("Res 5: " + " x = " + x.getArgumentValue() + ", " + e.getExpressionString() + " = " + e.calculate());
C++ code
#include "org/mariuszgromada/math/mxparser.hpp"
// ...
ArgumentPtr x = new_Argument("x = 0.5");
ExpressionPtr e = new_Expression("iff(x<1, 1; x<2, 2; x<3, 3; x<4, 4; x<5, 5)", x);
mXparser_consolePrintln("Res 1: " + " x = " + x->getArgumentValue() + ", " + e->getExpressionString() + " = " + e->calculate());
x->setArgumentValue(1.5);
mXparser_consolePrintln("Res 2: " + " x = " + x->getArgumentValue() + ", " + e->getExpressionString() + " = " + e->calculate());
x->setArgumentValue(2.5);
mXparser_consolePrintln("Res 3: " + " x = " + x->getArgumentValue() + ", " + e->getExpressionString() + " = " + e->calculate());
x->setArgumentValue(3.5);
mXparser_consolePrintln("Res 4: " + " x = " + x->getArgumentValue() + ", " + e->getExpressionString() + " = " + e->calculate());
x->setArgumentValue(4.5);
mXparser_consolePrintln("Res 5: " + " x = " + x->getArgumentValue() + ", " + e->getExpressionString() + " = " + e->calculate());
Code result
[mXparser-v.5.2.1] Res 1:  x = 0.5, iff(x<1, 1; x<2, 2; x<3, 3; x<4, 4; x<5, 5) = 1.0
[mXparser-v.5.2.1] Res 2:  x = 1.5, iff(x<1, 1; x<2, 2; x<3, 3; x<4, 4; x<5, 5) = 2.0
[mXparser-v.5.2.1] Res 3:  x = 2.5, iff(x<1, 1; x<2, 2; x<3, 3; x<4, 4; x<5, 5) = 3.0
[mXparser-v.5.2.1] Res 4:  x = 3.5, iff(x<1, 1; x<2, 2; x<3, 3; x<4, 4; x<5, 5) = 4.0
[mXparser-v.5.2.1] Res 5:  x = 4.5, iff(x<1, 1; x<2, 2; x<3, 3; x<4, 4; x<5, 5) = 5.0
Nuget – Package Manager (C#, F#, Visual Basic, …)

Install-Package MathParser.org-mXparser -Version 6.1.0

Nuget – .NET CLI

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

Nuget – Package Reference

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

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

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

Maven – Gradle

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

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.0
SOURCE_SUBDIR CURRENT/cpp/lib
)
FetchContent_MakeAvailable(
MathParserOrgMxParser)
target_link_libraries(YourExecutable
MathParserOrgMxParser)

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