Skip to content

Commit 6e74665

Browse files
committed
Fix EvalEx 3.6.1 API: evaluate(Expression,Token,EvaluationValue...), defaultConfiguration().withAdditionalFunctions()
1 parent 0c360a9 commit 6e74665

2 files changed

Lines changed: 58 additions & 62 deletions

File tree

core/src/main/java/cn/superiormc/ultimateshop/utils/MathFunctionTest.java

Lines changed: 49 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,8 @@
1010

1111
import java.math.BigDecimal;
1212
import java.math.RoundingMode;
13+
import java.util.Map;
1314

14-
/**
15-
* 独立测试 EvalEx + SIGMA 自定义函数 —— 不依赖 ConfigManager
16-
* 运行: java -cp "build/classes;deps/*" cn.superiormc.ultimateshop.utils.MathFunctionTest
17-
*/
1815
public class MathFunctionTest {
1916

2017
private static final int SIGMA_MAX_ITERATIONS = 100000;
@@ -23,24 +20,24 @@ public class MathFunctionTest {
2320
private static int failed;
2421

2522
static {
26-
config = ExpressionConfiguration.builder()
27-
.function("SIGMA", new SigmaFunction())
28-
.build();
23+
config = ExpressionConfiguration.defaultConfiguration()
24+
.withAdditionalFunctions(Map.entry("SIGMA", new SigmaFunction()));
2925
}
3026

3127
@FunctionParameter(name = "start")
3228
@FunctionParameter(name = "end")
3329
@FunctionParameter(name = "body")
3430
public static class SigmaFunction extends AbstractFunction {
3531
@Override
36-
public EvaluationValue evaluate(EvaluationValue... args) {
37-
int start = args[0].getNumberValue().intValue();
38-
int end = args[1].getNumberValue().intValue();
39-
String body = args[2].getStringValue();
32+
public EvaluationValue evaluate(Expression expression, Token functionToken,
33+
EvaluationValue... parameterValues) {
34+
int start = parameterValues[0].getNumberValue().intValue();
35+
int end = parameterValues[1].getNumberValue().intValue();
36+
String body = parameterValues[2].getStringValue();
4037
if (start > end) return EvaluationValue.numberValue(BigDecimal.ZERO);
4138
int count = end - start + 1;
4239
if (count > SIGMA_MAX_ITERATIONS) {
43-
throw new EvaluationException(new Token(0, body),
40+
throw new EvaluationException(functionToken,
4441
"SIGMA: too many iterations (" + count + ")");
4542
}
4643
BigDecimal sum = BigDecimal.ZERO;
@@ -78,9 +75,9 @@ static void check(String name, String expr, BigDecimal expected) {
7875
static BigDecimal d(String s) { return new BigDecimal(s); }
7976

8077
public static void main(String[] args) {
81-
System.out.println("= EvalEx + SIGMA 自定义函数 准确性测试 =\n");
78+
System.out.println("= EvalEx + SIGMA accuracy tests =\n");
8279

83-
System.out.println("--- 基础四则运算 ---");
80+
System.out.println("--- Basic arithmetic ---");
8481
check("1+1", "1+1", d("2"));
8582
check("3*4", "3*4", d("12"));
8683
check("10/3", "10/3", d("3.3333333333"));
@@ -89,68 +86,67 @@ public static void main(String[] args) {
8986
check("10%3", "10%3", d("1"));
9087
check("-5+8", "-5+8", d("3"));
9188
check("2.5*4", "2.5*4", d("10"));
92-
check("隐式乘法 2(3+4)", "2(3+4)", d("14"));
89+
check("implicit 2(3+4)", "2(3+4)", d("14"));
9390

94-
System.out.println("\n--- 内置数学函数 ---");
91+
System.out.println("\n--- Built-in functions ---");
9592
check("SQRT(16)", "SQRT(16)", d("4"));
9693
check("SQRT(2)", "SQRT(2)", d("1.4142135624"));
9794
check("ABS(-5)", "ABS(-5)", d("5"));
9895
check("ROUND(3.14159, 2)", "ROUND(3.14159, 2)", d("3.14"));
9996
check("FLOOR(3.9)", "FLOOR(3.9)", d("3"));
10097
check("CEILING(3.1)", "CEILING(3.1)", d("4"));
101-
check("LOG(2.718281828)", "LOG(2.718281828)", d("0.9999999993"));
98+
check("LOG(~e)", "LOG(2.718281828)", d("0.9999999993"));
10299
check("LOG10(100)", "LOG10(100)", d("2"));
103100
check("SIN(0)", "SIN(0)", d("0"));
104101
check("COS(0)", "COS(0)", d("1"));
105102
check("TAN(0)", "TAN(0)", d("0"));
106103
check("EXP(1)", "EXP(1)", d("2.7182818285"));
107104
check("FACT(5)", "FACT(5)", d("120"));
108-
check("MIN(3, 1, 4, 2)", "MIN(3, 1, 4, 2)", d("1"));
109-
check("MAX(3, 1, 4, 2)", "MAX(3, 1, 4, 2)", d("4"));
110-
111-
System.out.println("\n--- 布尔表达式 ---");
112-
check("1>0", "IF(1>0, 100, 0)", d("100"));
113-
check("1==1", "IF(1==1, 200, 0)", d("200"));
114-
check("1!=2", "IF(1!=2, 300, 0)", d("300"));
115-
check("AND", "IF(1<2 AND 2<3, 400, 0)", d("400"));
116-
check("OR", "IF(1>2 OR 2<3, 500, 0)", d("500"));
117-
check("NOT", "IF(NOT(1>2), 600, 0)", d("600"));
118-
119-
System.out.println("\n--- SIGMA 求和 ---");
120-
check("SIGMA(1, 10, \"i\") Σi", "SIGMA(1, 10, \"i\")", d("55"));
121-
check("SIGMA(1, 10, \"i*i\") Σi²", "SIGMA(1, 10, \"i*i\")", d("385"));
122-
check("SIGMA(1, 5, \"i^3\") Σi³", "SIGMA(1, 5, \"i^3\")", d("225"));
123-
check("SIGMA(1, 100, \"1\") counting","SIGMA(1, 100, \"1\")", d("100"));
124-
check("SIGMA(1, 4, \"FACT(i)\") Σi!", "SIGMA(1, 4, \"FACT(i)\")", d("33"));
125-
check("SIGMA(1, 1, \"42\") 单次", "SIGMA(1, 1, \"42\")", d("42"));
126-
check("SIGMA(0, 0, \"99\") 零索引", "SIGMA(0, 0, \"99\")", d("99"));
127-
check("SIGMA(5, 1, \"i\") start>end","SIGMA(5, 1, \"i\")", d("0"));
128-
129-
System.out.println("\n--- SIGMA 边界/嵌套 ---");
130-
check("SIGMA with EXP", "SIGMA(1, 5, \"EXP(-0.1*i)\")",
105+
check("MIN", "MIN(3, 1, 4, 2)", d("1"));
106+
check("MAX", "MAX(3, 1, 4, 2)", d("4"));
107+
108+
System.out.println("\n--- Boolean ---");
109+
check("IF 1>0", "IF(1>0, 100, 0)", d("100"));
110+
check("IF 1==1", "IF(1==1, 200, 0)", d("200"));
111+
check("IF 1!=2", "IF(1!=2, 300, 0)", d("300"));
112+
check("IF AND", "IF(1<2 AND 2<3, 400, 0)", d("400"));
113+
check("IF OR", "IF(1>2 OR 2<3, 500, 0)", d("500"));
114+
check("IF NOT", "IF(NOT(1>2), 600, 0)", d("600"));
115+
116+
System.out.println("\n--- SIGMA ---");
117+
check("SIGMA(1,10,\"i\") sum i", "SIGMA(1, 10, \"i\")", d("55"));
118+
check("SIGMA(1,10,\"i*i\") sum i^2", "SIGMA(1, 10, \"i*i\")", d("385"));
119+
check("SIGMA(1,5,\"i^3\") sum i^3", "SIGMA(1, 5, \"i^3\")", d("225"));
120+
check("SIGMA(1,100,\"1\") counting", "SIGMA(1, 100, \"1\")", d("100"));
121+
check("SIGMA(1,4,\"FACT(i)\") sum i!", "SIGMA(1, 4, \"FACT(i)\")", d("33"));
122+
check("SIGMA(1,1,\"42\") single", "SIGMA(1, 1, \"42\")", d("42"));
123+
check("SIGMA(0,0,\"99\") zero index","SIGMA(0, 0, \"99\")", d("99"));
124+
check("SIGMA(5,1,\"i\") start>end", "SIGMA(5, 1, \"i\")", d("0"));
125+
126+
System.out.println("\n--- SIGMA advanced ---");
127+
check("SIGMA + EXP", "SIGMA(1, 5, \"EXP(-0.1*i)\")",
131128
d("4.4959131450"));
132-
check("SIGMA with SQRT", "SIGMA(1, 9, \"SQRT(i)\")",
129+
check("SIGMA + SQRT", "SIGMA(1, 9, \"SQRT(i)\")",
133130
d("19.3060005260"));
134-
check("嵌套 IF", "SIGMA(1, 10, \"IF(i%2==0, i, 0)\")",
131+
check("SIGMA + IF", "SIGMA(1, 10, \"IF(i%2==0, i, 0)\")",
135132
d("30"));
136-
check("大范围 sigma", "SIGMA(1, 1000, \"1\")",
133+
check("SIGMA large", "SIGMA(1, 1000, \"1\")",
137134
d("1000"));
138135

139-
System.out.println("\n--- 文章中的实用公式 ---");
140-
check("e^{-lambda*n} λ=0.1 n=10", "EXP(-0.1*10)", d("0.3678794412"));
141-
check("round{x, 2}", "ROUND(3.14159, 2)", d("3.14"));
142-
check("floor{x}", "FLOOR(3.9)", d("3"));
143-
check("1-e^{-λn} λ=0.1 n=5", "1-EXP(-0.1*5)", d("0.3934693403"));
144-
check("(t-μ)^6/(2σ²) t=10 μ=5 σ=2","(10-5)^6/(2*2^2)", d("1953.125"));
145-
check("exp[-(t-μ)^6/(2σ²)]", "EXP(-(10-5)^6/(2*2^2))", d("0.0000000000"));
136+
System.out.println("\n--- Article formulas ---");
137+
check("e^{-lambda*n}", "EXP(-0.1*10)", d("0.3678794412"));
138+
check("ROUND(pi,2)", "ROUND(3.14159, 2)", d("3.14"));
139+
check("FLOOR", "FLOOR(3.9)", d("3"));
140+
check("1-e^{-ln}", "1-EXP(-0.1*5)", d("0.3934693403"));
141+
check("(t-u)^6/(2s^2)","(10-5)^6/(2*2^2)", d("1953.125"));
146142

147143
System.out.println("\n" + "=".repeat(40));
148-
System.out.println("总计: " + (passed + failed) + " 项, 通过 " + passed + ", 失败 " + failed);
144+
System.out.println("Total: " + (passed + failed) + ", PASS: " + passed + ", FAIL: " + failed);
149145
if (failed > 0) {
150-
System.out.println(" 存在失败用例!");
146+
System.out.println("SOME TESTS FAILED!");
151147
System.exit(1);
152148
} else {
153-
System.out.println(" 全部通过!");
149+
System.out.println("ALL PASSED!");
154150
}
155151
}
156152
}

core/src/main/java/cn/superiormc/ultimateshop/utils/MathUtil.java

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import java.text.DecimalFormat;
1616
import java.text.DecimalFormatSymbols;
1717
import java.util.Locale;
18+
import java.util.Map;
1819

1920
public class MathUtil {
2021

@@ -36,9 +37,8 @@ public static void init() {
3637
integerFormat = new DecimalFormat(integerPattern, symbols);
3738
decimalFormat = new DecimalFormat(decimalPattern, symbols);
3839

39-
expressionConfig = ExpressionConfiguration.builder()
40-
.function("SIGMA", new SigmaFunction())
41-
.build();
40+
expressionConfig = ExpressionConfiguration.defaultConfiguration()
41+
.withAdditionalFunctions(Map.entry("SIGMA", new SigmaFunction()));
4242
}
4343

4444
public static double multiply(double left, double right) {
@@ -91,19 +91,19 @@ public static BigDecimal doCalculate(String mathStr, int scale) {
9191
public static class SigmaFunction extends AbstractFunction {
9292

9393
@Override
94-
public EvaluationValue evaluate(EvaluationValue... args) {
95-
int start = args[0].getNumberValue().intValue();
96-
int end = args[1].getNumberValue().intValue();
97-
String body = args[2].getStringValue();
94+
public EvaluationValue evaluate(Expression expression, Token functionToken,
95+
EvaluationValue... parameterValues) {
96+
int start = parameterValues[0].getNumberValue().intValue();
97+
int end = parameterValues[1].getNumberValue().intValue();
98+
String body = parameterValues[2].getStringValue();
9899

99100
if (start > end) {
100101
return EvaluationValue.numberValue(BigDecimal.ZERO);
101102
}
102103

103104
int count = end - start + 1;
104105
if (count > SIGMA_MAX_ITERATIONS) {
105-
throw new EvaluationException(
106-
new Token(0, body),
106+
throw new EvaluationException(functionToken,
107107
"SIGMA: too many iterations (" + count + "), maximum is " + SIGMA_MAX_ITERATIONS);
108108
}
109109

0 commit comments

Comments
 (0)