Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's a good library for parsing mathematical expressions in java? [closed]

Tags:

I'm an Android Developer and as part of my next app I will need to evaluate a large variety of user created mathematical expressions and equations. I am looking for a good java library that is lightweight and can evaluate mathematical expressions using user defined variables and constants, trig and exponential functions, etc.

I've looked around and Jep seems to be popular, but I would like to hear more suggestions, especially from people who have used these libraries before.

like image 481
CodeFusionMobile Avatar asked Feb 09 '10 05:02

CodeFusionMobile


People also ask

What is Java expression parser?

JEP is a Java API for parsing and evaluating mathematical expressions. With this library you can allow your users to enter an arbitrary formula as a string, and instantly evaluate it. JEP supports user defined variables, constants, and functions. A number of common mathematical functions and constants are included.

How do you read an expression in Java?

Implement the power of regular expressions. You should use Scanner. nextLine() to get the full line that was input into the console. Manipulate the String after you read in the line from the Scanner .


2 Answers

I wrote a simple but capable Math Expression Evaluator a while back, which is free and open-source. It's main advantage is being fast and tiny - both are a good thing with hand-held devices. If it meets your need you are welcome to use it.

Primary Features:

  • Basic math operators, with inferred precedence (^ * × · / ÷ % + -).
  • Explicit precedence with parenthesis.
  • Implicit multiplication of bracketed subexpressions.
  • Correct right-associativity of exponentials (power operator).
  • Direct support for hexadecimal numbers prefixed by 0x.
  • Constants and variables.
  • Extensible operators.
  • Extensible functions.
  • 20 KiB footprint.

Example

MathEval math=new MathEval();

math.setVariable("Top",    5);
math.setVariable("Left",  20);
math.setVariable("Bottom",15);
math.setVariable("Right", 60);

System.out.println("Middle: "+math.evaluate("floor((Right+1-Left)/2)"));
like image 107
Lawrence Dol Avatar answered Sep 29 '22 09:09

Lawrence Dol


JEval is a good alternative. I abandoned Jep due to it becoming commercial. The only concern is that JEval seems to be a little dormant at the moment (last release in 2008).

like image 32
gpampara Avatar answered Sep 29 '22 10:09

gpampara