https://blog.csdn.net/weixin_40461281/article/details/105584570
2.X 存在精确度问题 推荐使用3.X 参考文章: java 使用 jep3.5 解析公式自动计算(包含BigDecimal模式 浮点数精准计算)
1.增加jep依赖
-
<dependency>
-
<groupId>org.scijava </groupId>
-
<artifactId>jep </artifactId>
-
<version>2.4.2 </version>
-
</dependency>
2.计算公式
JEP本身支持的函数 如果没有需要的函数 下文有自定义函数实现
计算 M12*3.14/4*pow(O5,2)*(K11+273-G11)/(G12*sqrt(3.14*M11*P11)) 的值 够复杂吧
-
public static void main(String[] args) {
-
JEP jep = new JEP();
-
// 添加常用函数
-
jep.addStandardFunctions();
-
// 添加常用常量
-
jep.addStandardConstants();
-
-
String exp = "M12*3.14/4*pow(O5,2)*(K11+273-G11)/(G12*sqrt(3.14*M11*P11))"; //给变量赋值
-
jep.addVariable( "M12", 1.1);
-
jep.addVariable( "O5", 11.28665296);
-
jep.addVariable( "K11", 25);
-
jep.addVariable( "G11", 200);
-
jep.addVariable( "G12", 100000);
-
jep.addVariable( "M11", 0.000000129);
-
jep.addVariable( "P11", 10);
-
-
try { //执行
-
jep.parseExpression(exp);
-
double result = jep.getValue();
-
System.out.println( "计算结果: " + result);
-
} catch (Throwable e) {
-
System.out.println( "An error occured: " + e.getMessage());
-
}
-
}
完全不是问题
3.实现自定义函数 min max
min函数 两数取最小值
-
public class Min extends PostfixMathCommand {
-
public Min() {
-
super();
-
// 使用参数的数量
-
numberOfParameters = 2;
-
}
-
-
-
public void run(Stack inStack) throws ParseException {
-
//检查栈
-
checkStack(inStack);
-
Object param2 = inStack.pop();
-
Object param1 = inStack.pop();
-
-
if ((param1 instanceof Number) && (param2 instanceof Number)) {
-
double p1 = ((Number) param2).doubleValue();
-
double p2 = ((Number) param1).doubleValue();
-
-
double result = Math.min(p1, p2);
-
-
inStack.push( new Double(result));
-
} else {
-
throw new ParseException( "Invalid parameter type");
-
}
-
return;
-
}
-
-
}
max函数 两数取最大值
-
static class Max extends PostfixMathCommand {
-
public Max() {
-
super();
-
// 使用参数的数量
-
numberOfParameters = 2;
-
}
-
-
-
public void run(Stack inStack) throws ParseException {
-
//检查栈
-
checkStack(inStack);
-
Object param2 = inStack.pop();
-
Object param1 = inStack.pop();
-
-
if ((param1 instanceof Number) && (param2 instanceof Number)) {
-
double p1 = ((Number) param2).doubleValue();
-
double p2 = ((Number) param1).doubleValue();
-
-
double result = Math.max(p1, p2);
-
-
inStack.push( new Double(result));
-
} else {
-
throw new ParseException( "Invalid parameter type");
-
}
-
return;
-
}
-
-
}
验证是否好用
-
public static void main(String[] args) {
-
JEP jep = new JEP();
-
// 添加常用函数
-
jep.addStandardFunctions();
-
// 添加常用常量
-
jep.addStandardConstants();
-
// 添加自定义函数
-
jep.addFunction( "min", new Min());
-
jep.addFunction( "max", new Max());
-
-
String min = "min(A1,A2)";
-
String max = "max(A1,A2)";
-
jep.addVariable( "A1", 1.1);
-
jep.addVariable( "A2", 2.3);
-
-
try { //执行
-
jep.parseExpression(min);
-
double minResult = jep.getValue();
-
jep.parseExpression(max);
-
double maxResult = jep.getValue();
-
System.out.println( "最小值为: " + minResult);
-
System.out.println( "最大值为: " + maxResult);
-
} catch (Throwable e) {
-
System.out.println( "An error occured: " + e.getMessage());
-
}
-
}
完全没问题