Skip to content

Commit 15e2d43

Browse files
author
linzhijun
committed
fix
1 parent 734efce commit 15e2d43

24 files changed

Lines changed: 96 additions & 81 deletions

‎JavaScript/src/Internals/Functions/MathBase/Function_BESSELI.js‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,12 @@ class Function_BESSELI extends Function_2 {
1919
let x = args1.NumberValue;
2020
let n = Math.trunc(args2.NumberValue);
2121

22-
return Operand.Create(this.besselI(n, x));
22+
let result = this.besselI(n, x);
23+
if (!isFinite(result)) {
24+
// 对齐 C# OverflowException → FunctionError
25+
return this.functionError();
26+
}
27+
return Operand.Create(result);
2328
}
2429

2530
besselI(n, x) {

‎JavaScript/src/Internals/Functions/MathBase/Function_ERF.js‎

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -18,20 +18,16 @@ class Function_ERF extends Function_1 {
1818
}
1919

2020
erf(x) {
21-
let a1 = 0.254829592;
22-
let a2 = -0.284496736;
23-
let a3 = 1.421413741;
24-
let a4 = -1.453152027;
25-
let a5 = 1.061405429;
26-
let p = 0.3275911;
27-
28-
let sign = x < 0 ? -1 : 1;
29-
x = Math.abs(x);
30-
31-
let t = 1.0 / (1.0 + p * x);
32-
let y = 1.0 - (((((a5 * t + a4) * t) + a3) * t + a2) * t + a1) * t * Math.exp(-x * x);
21+
// 用高精度补余误差函数合成,避免 A&S 逼近在大 x 时精度丢失
22+
return x >= 0 ? 1.0 - this.erfc(x) : this.erfc(-x) - 1.0;
23+
}
3324

34-
return sign * y;
25+
erfc(x) {
26+
// Numerical Recipes 6.2 erfcc:对 |x| 做有理逼近
27+
let z = Math.abs(x);
28+
let t = 1.0 / (1.0 + 0.5 * z);
29+
let ans = t * Math.exp(-z * z - 1.26551223 + t * (1.00002368 + t * (0.37409196 + t * (0.09678418 + t * (-0.18628806 + t * (0.27886807 + t * (-1.13520398 + t * (1.48851587 + t * (-0.82215223 + t * 0.17087277)))))))));
30+
return x >= 0 ? ans : 2.0 - ans;
3531
}
3632
}
3733

‎JavaScript/src/Internals/Functions/MathBase/Function_ERFC.js‎

Lines changed: 5 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -18,24 +18,11 @@ class Function_ERFC extends Function_1 {
1818
}
1919

2020
erfc(x) {
21-
return 1.0 - this.erf(x);
22-
}
23-
24-
erf(x) {
25-
let a1 = 0.254829592;
26-
let a2 = -0.284496736;
27-
let a3 = 1.421413741;
28-
let a4 = -1.453152027;
29-
let a5 = 1.061405429;
30-
let p = 0.3275911;
31-
32-
let sign = x < 0 ? -1 : 1;
33-
x = Math.abs(x);
34-
35-
let t = 1.0 / (1.0 + p * x);
36-
let y = 1.0 - (((((a5 * t + a4) * t) + a3) * t + a2) * t + a1) * t * Math.exp(-x * x);
37-
38-
return sign * y;
21+
// Numerical Recipes 6.2 erfcc:对 |x| 做有理逼近,避免 1-erf(x) 在大 x 时精度归零
22+
let z = Math.abs(x);
23+
let t = 1.0 / (1.0 + 0.5 * z);
24+
let ans = t * Math.exp(-z * z - 1.26551223 + t * (1.00002368 + t * (0.37409196 + t * (0.09678418 + t * (-0.18628806 + t * (0.27886807 + t * (-1.13520398 + t * (1.48851587 + t * (-0.82215223 + t * 0.17087277)))))))));
25+
return x >= 0 ? ans : 2.0 - ans;
3926
}
4027
}
4128

‎JavaScript/src/Internals/Functions/MathSum/Function_BETADIST.js‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,9 @@ class Function_BETADIST extends Function_3 {
2525
let alpha = args2.DoubleValue;
2626
let beta = args3.DoubleValue;
2727

28-
if (alpha < 0.0 || beta < 0.0) {
29-
return this.functionError();
30-
}
28+
if (x < 0.0 || x > 1.0) { return this.parameterError(1); }
29+
if (alpha <= 0.0) { return this.parameterError(2); }
30+
if (beta <= 0.0) { return this.parameterError(3); }
3131
return Operand.Create(ExcelFunctions.betaDist(x, alpha, beta));
3232
}
3333
}

‎JavaScript/src/Internals/Functions/MathSum/Function_BETAINV.js‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,9 @@ class Function_BETAINV extends Function_3 {
2424
let p = args1.DoubleValue;
2525
let alpha = args2.DoubleValue;
2626
let beta = args3.DoubleValue;
27-
if (alpha < 0.0 || beta < 0.0 || p < 0.0 || p > 1.0) {
28-
return this.functionError();
29-
}
27+
if (p < 0.0 || p > 1.0) { return this.parameterError(1); }
28+
if (alpha <= 0.0) { return this.parameterError(2); }
29+
if (beta <= 0.0) { return this.parameterError(3); }
3030
return Operand.Create(ExcelFunctions.betaInv(p, alpha, beta));
3131
}
3232
}

‎JavaScript/src/Internals/Functions/MathSum/Function_BINOMDIST.js‎

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,13 @@ class Function_BINOMDIST extends Function_4 {
2424
let args4 = this.getBoolean_4(work, tempParameter);
2525
if (args4.IsError) return args4;
2626

27+
let k = args1.IntValue;
2728
let n2 = args2.IntValue;
2829
let n3 = args3.DoubleValue;
29-
if (!(n3 >= 0.0 && n3 <= 1.0 && n2 >= 0)) {
30-
return this.functionError();
31-
}
32-
return Operand.Create(ExcelFunctions.binomDist(args1.IntValue, n2, n3, args4.BooleanValue));
30+
if (n2 < 0) { return this.parameterError(2); }
31+
if (k < 0 || k > n2) { return this.parameterError(1); }
32+
if (n3 < 0.0 || n3 > 1.0) { return this.parameterError(3); }
33+
return Operand.Create(ExcelFunctions.binomDist(k, n2, n3, args4.BooleanValue));
3334
}
3435
}
3536

‎JavaScript/src/Internals/Functions/MathSum/Function_EXPONDIST.js‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,14 @@ class Function_EXPONDIST extends Function_3 {
2222
if (args3.IsError) return args3;
2323

2424
let n1 = args1.DoubleValue;
25+
let lambda = args2.DoubleValue;
2526
if (n1 < 0.0) {
26-
return this.functionError();
27+
return this.parameterError(1);
2728
}
28-
return Operand.Create(ExcelFunctions.exponDist(n1, args2.DoubleValue, args3.BooleanValue));
29+
if (!(lambda > 0.0)) {
30+
return this.parameterError(2);
31+
}
32+
return Operand.Create(ExcelFunctions.exponDist(n1, lambda, args3.BooleanValue));
2933
}
3034
}
3135

‎JavaScript/src/Internals/Functions/MathSum/Function_FDIST.js‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,9 @@ class Function_FDIST extends Function_3 {
2424
let x = args1.DoubleValue;
2525
let degreesFreedom = args2.IntValue;
2626
let degreesFreedom2 = args3.IntValue;
27-
if (degreesFreedom <= 0.0 || degreesFreedom2 <= 0.0) {
28-
return this.functionError();
29-
}
27+
if (x <= 0.0) { return this.parameterError(1); }
28+
if (degreesFreedom <= 0) { return this.parameterError(2); }
29+
if (degreesFreedom2 <= 0) { return this.parameterError(3); }
3030
return Operand.Create(ExcelFunctions.fDist(x, degreesFreedom, degreesFreedom2));
3131
}
3232
}

‎JavaScript/src/Internals/Functions/MathSum/Function_FINV.js‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,9 @@ class Function_FINV extends Function_3 {
2424
let p = args1.DoubleValue;
2525
let degreesFreedom = args2.IntValue;
2626
let degreesFreedom2 = args3.IntValue;
27-
if (degreesFreedom <= 0.0 || degreesFreedom2 <= 0.0 || p < 0.0 || p > 1.0) {
28-
return this.functionError();
29-
}
27+
if (p <= 0.0 || p >= 1.0) { return this.parameterError(1); }
28+
if (degreesFreedom <= 0) { return this.parameterError(2); }
29+
if (degreesFreedom2 <= 0) { return this.parameterError(3); }
3030
return Operand.Create(ExcelFunctions.fInv(p, degreesFreedom, degreesFreedom2));
3131
}
3232
}

‎JavaScript/src/Internals/Functions/MathSum/Function_FISHER.js‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ class Function_FISHER extends Function_1 {
1515
if (args1.IsError) { return args1; }
1616
let x = args1.DoubleValue;
1717
if (x >= 1 || x <= -1) {
18-
return this.functionError();
18+
return this.parameterError(1);
1919
}
2020
let n = 0.5 * Math.log((1 + x) / (1 - x));
2121
return Operand.Create(n);

0 commit comments

Comments
 (0)