Skip to content

A simple equation parser for Java

License

Notifications You must be signed in to change notification settings

Sematre/MathParser

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

MathParser

Release Version Maven Version Build Status License

A simple equation parser for Java.

Code example

Minimal

This code calculates the sum of "2+2". (= 4)

String equation = "2+2";

MathParser mathParser = new MathParser();
System.out.println(mathParser.parse(equation));

Advanced

Brackets? No problem! "18*(36-50)" = -252

String equation = "18*(36-50)";

MathParser mathParser = new MathParser();
System.out.println(mathParser.parse(equation));

Functions

You need functions? Too easy! "root(3,8)" (3th root of 8) = 2

String equation = "root(3,8)";

MathParser mathParser = new MathParser();
System.out.println(mathParser.parse(equation));

Custom functions

Not enough functions? Try this! fooBar(1,2,3) = 1 + 2 + 3 = 6

String equation = "fooBar(1,2,3)";

MathParser mathParser = new MathParser();
mathParser.addFunction("fooBar", new MathFunction() {

	@Override
	public Double execute(Double[] parameter) {
		return parameter[0] + parameter[1] + parameter[2];
	}
});

System.out.println(mathParser.parse(equation));

CrAZy foRMAtTING

Still no problem! 5((2+4)-2)+sin(root(1)*pi) = 20

String equation = "((  (5*     ((2   +4)-2)   +sin(root(2,1)*pi())))";

MathParser mathParser = new MathParser();
System.out.println(mathParser.parse(equation));

Implementation

Gradle:

dependencies {
	implementation 'de.sematre.mathparser:MathParser:1.1'
}

Maven:

<dependency>
	<groupId>de.sematre.mathparser</groupId>
	<artifactId>MathParser</artifactId>
	<version>1.1</version>
</dependency>

Release History

  • 1.1
    • Code cleanup
  • 1.0
    • Initial version

Info

© Sematre 2019

Distributed under the MIT License. See LICENSE for more information.