Fractions

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: Fraction (proper) as Number Literal

Java/C# code
// JAVA: import org.mariuszgromada.math.mxparser.*;
// C#: using org.mariuszgromada.math.mxparser;
// ...
Expression e = new Expression("1_2");

mXparser.consolePrintln(e.getExpressionString() + " = " + e.calculate());
C++ code
#include "org/mariuszgromada/math/mxparser.hpp"
// ...
ExpressionPtr e = new_Expression("1_2");

mXparser::consolePrintln(e->getExpressionString() + " = " + e->calculate());
Code result
[mXparser-v.5.2.1] 1_2 = 0.5

Case 2: Improper Fraction as Number Literal

Java/C# code
// JAVA: import org.mariuszgromada.math.mxparser.*;
// C#: using org.mariuszgromada.math.mxparser;
// ...
Expression e = new Expression("-6_5");

mXparser.consolePrintln(e.getExpressionString() + " = " + e.calculate());
C++ code
#include "org/mariuszgromada/math/mxparser.hpp"
// ...
ExpressionPtr e = new_Expression("-6_5");

mXparser::consolePrintln(e->getExpressionString() + " = " + e->calculate());
Code result
[mXparser-v.5.2.1] -6_5 = -1.2

Case 3: Fraction (Mixed Number) as Number Literal

Java/C# code
// JAVA: import org.mariuszgromada.math.mxparser.*;
// C#: using org.mariuszgromada.math.mxparser;
// ...
Expression e = new Expression("2_3_5");

mXparser.consolePrintln(e.getExpressionString() + " = " + e.calculate());
C++ code
#include "org/mariuszgromada/math/mxparser.hpp"
// ...
ExpressionPtr e = new_Expression("2_3_5");

mXparser::consolePrintln(e->getExpressionString() + " = " + e->calculate());
Code result
[mXparser-v.5.2.1] 2_3_5 = 2.6

Case 4: Fraction (Mixed Numer) and Improper Fraction in one Number Literal

Java/C# code
// JAVA: import org.mariuszgromada.math.mxparser.*;
// C#: using org.mariuszgromada.math.mxparser;
// ...
Expression e = new Expression("2_12_5");

mXparser.consolePrintln(e.getExpressionString() + " = " + e.calculate());
C++ code
#include "org/mariuszgromada/math/mxparser.hpp"
// ...
ExpressionPtr e = new_Expression("2_12_5");

mXparser::consolePrintln(e->getExpressionString() + " = " + e->calculate());
Code result
[mXparser-v.5.2.1] 2_12_5 = 4.4

Case 5: Operations on Fractions

Java/C# code
// JAVA: import org.mariuszgromada.math.mxparser.*;
// C#: using org.mariuszgromada.math.mxparser;
// ...
Expression e = new Expression("1_2-6_5*2_3_5/2_12_5");

mXparser.consolePrintln(e.getExpressionString() + " = " + e.calculate());
C++ code
#include "org/mariuszgromada/math/mxparser.hpp"
// ...
ExpressionPtr e = new_Expression("1_2-6_5*2_3_5/2_12_5");

mXparser::consolePrintln(e->getExpressionString() + " = " + e->calculate());
Code result
[mXparser-v.5.2.1] 1_2-6_5*2_3_5/2_12_5 = -0.2090909090909091

Case 6: Represent double as Fraction

Java/C# code
// JAVA: import org.mariuszgromada.math.mxparser.*;
// C#: using org.mariuszgromada.math.mxparser;
// ...
Expression e = new Expression("1_2-6_5*2_3_5/2_12_5");
double resultAsDouble = e.calculate();
String resultAsFraction = mXparser.toFractionString(resultAsDouble);

mXparser.consolePrintln(e.getExpressionString() + " = " + resultAsFraction);
C++ code
#include "org/mariuszgromada/math/mxparser.hpp"
// ...
ExpressionPtr e = new_Expression("1_2-6_5*2_3_5/2_12_5");
double resultAsDouble = e->calculate();
StringPtr resultAsFraction = mXparser::toFractionString(resultAsDouble);

mXparser::consolePrintln(e->getExpressionString() + " = " + resultAsFraction);
Code result
[mXparser-v.5.2.1] 1_2-6_5*2_3_5/2_12_5 = -23/110
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