### exp4j: a simple mathematical expression parser for java

#### posted by fas on 2010-12-22 . Tagged as java, programming, projects, math, exp4j

System.out.println(PostfixExpression.fromInfix("2 * 17.41 + (12*2)^(0-1)").calculate()); > 34.861666666666665

define a function with two variables x and y:

double x=Math.E; double y=Math.PI; System.out.println(PostfixExpression.fromInfix("f(x,y)=x^2 - 2 * y").calculate(x,y)); > 1.1058707917510633

only thing to worry about is to pass the argument values to the calculate method in the same

order as the variable names in the function expression. so if instead we would have written "f(pi,e)=e^2 - 2 * pi" we would have to reverse the arguments for the calculate method. i.e. "calculate(pi,e)".

A simple commandline interpreter has also been added to the library:

java -jar exp4j.jar "3+4-log(23.2)^(2-1) * -1" > 10.144152278672264

The following operations and functions are supported:

Functions are delegated to java.lang.Math, and named accordingly.

- Addition
- Subtraction
- Multiplication
- Divison
- Modulo
- Exponentiation
- Unary minus, plus
**abs**: absolute value**acos**: arc cosine**asin**: arc sine**atan**: arc tangent**cbrt**: cubic root**ceil**: next integer value**cos**: the cosine**cosh**: hyperbolic cosine**exp**: euler's number raised to the value (e^x)**floor**: previous integer value**log**: logarithmus naturalis (base e)**sin**: sine**sinh**: hyerbolic sine**sqrt**: square root**tan**: tangent**tanh**: hyperbolic tangent

exp4j is published under the Apache License Version 2.0

**Download exp4j**.

if you find any bugs please post them here or drop me a mail and expect my eternal gratitude.

Tags: java, programming, projects, math, exp4j