`
rensanning
  • 浏览: 3509814 次
  • 性别: Icon_minigender_1
  • 来自: 大连
博客专栏
Efef1dba-f7dd-3931-8a61-8e1c76c3e39f
使用Titanium Mo...
浏览量:37407
Bbab2146-6e1d-3c50-acd6-c8bae29e307d
Cordova 3.x入门...
浏览量:603972
C08766e7-8a33-3f9b-9155-654af05c3484
常用Java开源Libra...
浏览量:677512
77063fb3-0ee7-3bfa-9c72-2a0234ebf83e
搭建 CentOS 6 服...
浏览量:86989
E40e5e76-1f3b-398e-b6a6-dc9cfbb38156
Spring Boot 入...
浏览量:399568
Abe39461-b089-344f-99fa-cdfbddea0e18
基于Spring Secu...
浏览量:68983
66a41a70-fdf0-3dc9-aa31-19b7e8b24672
MQTT入门
浏览量:90294
社区版块
存档分类
最新评论

Java数学表达式计算(Expression Evaluator)

    博客分类:
  • Java
 
阅读更多
常见的表达式计算lib有:


(1)parsii
String exp = "2 + (7-5) * 3.14159 * x + sin(0)";

// compile
Scope scope = Scope.create();
Expression parsiiExpr = Parser.parse(exp);
Variable var = scope.getVariable("x");
var.setValue(X_VALUE);

// evaluate
double result = parsiiExpr.evaluate();

System.out.println(result);//-> 2.0


(2)JEval
String exp = "2 + (7-5) * 3.14159 * #{x} + sin(0)";

// compile
Evaluator jevalEvaluator = new Evaluator();
jevalEvaluator.setVariables(Collections.singletonMap("x", Double.toString(X_VALUE)));

// evaluate
double result = Double.parseDouble(jevalEvaluator.evaluate(exp));

System.out.println(result);//-> 2.0


(3)JEPLite
String exp = "2 + (7-5) * 3.14159 * x + sin(0)";

// compile
JEP jep = new JEP();
jep.addVariable("x", X_VALUE);
jep.parseExpression(exp);
DoubleStack jepStack = new DoubleStack();

// evaluate
double result = jep.getValue(jepStack);

System.out.println(result);//-> 2.0


http://andreas.haufler.info/2013/12/how-to-write-one-of-fastest-expression.html
http://www.transylvania-jug.org/archives/5777
分享到:
评论
2 楼 imGaofy 2015-02-11  
imGaofy 写道
引用

String exp = "2 + (7-5) * 3.14159 * x + sin(0)";

// compile
Scope scope = Scope.create();
Expression parsiiExpr = Parser.parse(exp);
Variable var = scope.getVariable("x");
var.setValue(X_VALUE);

// evaluate
double result = parsiiExpr.evaluate();

System.out.println(result);//-> 2.0


好像这使用有点问题, 应该为 :
Expression parsiiExpr = Parser.parse(exp); --> Expression parsiiExpr = Parser.parse(exp, scope);

可能是版本原因, 我的版本为 parsii-1.4.jar
1 楼 imGaofy 2015-02-11  
引用

String exp = "2 + (7-5) * 3.14159 * x + sin(0)";

// compile
Scope scope = Scope.create();
Expression parsiiExpr = Parser.parse(exp);
Variable var = scope.getVariable("x");
var.setValue(X_VALUE);

// evaluate
double result = parsiiExpr.evaluate();

System.out.println(result);//-> 2.0


好像这使用有点问题, 应该为 :
Expression parsiiExpr = Parser.parse(exp); --> Expression parsiiExpr = Parser.parse(exp, scope);

相关推荐

Global site tag (gtag.js) - Google Analytics