Skip to content

Commit f6af513

Browse files
author
linzhijun
committed
fix
1 parent 3e85e2a commit f6af513

28 files changed

Lines changed: 217 additions & 61 deletions

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,11 @@ class Function_CEILING extends Function_2 {
2424
if (b == 0) {
2525
return Operand.Zero;
2626
}
27-
if (b < 0) {
28-
return this.parameterError(2);
29-
}
3027

3128
let a = args1.NumberValue;
29+
if (b < 0 && a > 0) {
30+
return this.parameterError(1);
31+
}
3232
let d = Math.ceil(a / b) * b;
3333
return Operand.Create(d);
3434
}

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,12 @@ class Function_COMBIN extends Function_2 {
1818

1919
let total = args1.IntValue;
2020
let count = args2.IntValue;
21-
if (total < 0 || count < 0 || total < count) {
21+
if (total < 0) {
2222
return this.parameterError(1);
2323
}
24+
if (count < 0 || total < count) {
25+
return this.parameterError(2);
26+
}
2427
let sum = 1;
2528
let sum2 = 1;
2629
for (let i = 0; i < count; i++) {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ class Function_DELTA extends Function_N {
1111
}
1212

1313
evaluate(engine, tempParameter) {
14-
if (this.z.length < 1) {
14+
if (this.z.length < 1 || this.z.length > 2) {
1515
return this.functionError();
1616
}
1717

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ class Function_EXP extends Function_1 {
1313
evaluate(engine, tempParameter) {
1414
let args1 = this.getNumber_1(engine, tempParameter);
1515
if (args1.IsError) { return args1; }
16+
if (args1.NumberValue > 700) {
17+
return this.parameterError(1);
18+
}
1619
return Operand.Create(Math.exp(args1.NumberValue));
1720
}
1821
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ class Function_FACT extends Function_1 {
1515
if (args1.IsError) { return args1; }
1616

1717
let z = args1.IntValue;
18-
if (z < 0) {
19-
return this.functionError();
18+
if (z < 0 || z > 28) {
19+
return this.parameterError(1);
2020
}
2121
let d = 1;
2222
for (let i = 1; i <= z; i++) {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ class Function_FACTDOUBLE extends Function_1 {
1414
let args1 = this.getNumber_1(engine, tempParameter);
1515
if (args1.IsError) { return args1; }
1616
let z = args1.IntValue;
17-
if (z < 0) { return this.functionError(); }
17+
if (z < 0 || z > 45) { return this.parameterError(1); }
1818

1919
let d = 1;
2020
for (let i = z; i > 0; i -= 2) {

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

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,53 @@
11
import { Function_3 } from '../Function_3.js';
22
import { Operand } from '../../../Operand.js';
33

4+
// 模拟 C# Math.Round(x, 0, MidpointRounding.AwayFromZero)
5+
function roundAwayFromZero(x) {
6+
return x < 0 ? -Math.round(-x) : Math.round(x);
7+
}
8+
49
class Function_FIXED extends Function_3 {
510
get Name() {
611
return "Fixed";
712
}
813

914
constructor(z) {
10-
super(z);
11-
}
15+
super(z);
16+
}
1217

1318
evaluate(engine, tempParameter) {
1419
let args1 = this.getNumber_1(engine, tempParameter);
1520
if (args1.IsError) { return args1; }
16-
21+
1722
let num = 2;
1823
if (this.b !== null && this.b !== undefined) {
1924
let args2 = this.getNumber_2(engine, tempParameter);
2025
if (args2.IsError) { return args2; }
2126
num = args2.IntValue;
27+
// Excel 支持负数 decimals(向左取整),如 FIXED(1234.567,-1)="1,230",范围与 ROUND 一致
28+
if (num < -15 || num > 15) {
29+
return this.parameterError(2);
30+
}
2231
}
2332

24-
// 四舍五入到指定小数位
25-
let s = Math.round(args1.NumberValue * Math.pow(10, num)) / Math.pow(10, num);
33+
let s = args1.NumberValue;
34+
if (num >= 0) {
35+
let factor = Math.pow(10, num);
36+
s = roundAwayFromZero(s * factor) / factor;
37+
} else {
38+
// Math.Round(decimal, int) 只支持非负位数,负数位数(向左取整)改用先除后乘
39+
let factor = Math.pow(10, -num);
40+
s = roundAwayFromZero(s / factor) * factor;
41+
}
2642
let no = false;
2743
if (this.c !== null && this.c !== undefined) {
2844
let args3 = this.getBoolean_3(engine, tempParameter);
2945
if (args3.IsError) { return args3; }
3046
no = args3.BooleanValue;
3147
}
3248
if (no === false) {
33-
// 格式化数字,保留指定小数位数并添加千位分隔符
34-
let formatted = s.toFixed(num);
35-
// 添加千位分隔符
49+
// 负数位数取整后无小数位,用 0 位小数保持千分位
50+
let formatted = (num < 0 ? s.toFixed(0) : s.toFixed(num));
3651
let parts = formatted.split('.');
3752
parts[0] = parts[0].replace(/\B(?=(\d{3})+(?!\d))/g, ',');
3853
return Operand.Create(parts.join('.'));
@@ -42,4 +57,3 @@ class Function_FIXED extends Function_3 {
4257
}
4358

4459
export { Function_FIXED };
45-

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@ class Function_FLOOR extends Function_2 {
2626
}
2727

2828
let a = args1.NumberValue;
29+
if (b < 0 && a > 0) {
30+
return this.parameterError(1);
31+
}
2932
let d = Math.floor(a / b) * b;
3033
return Operand.Create(d);
3134
}

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,11 @@ class Function_GCD extends Function_N {
2525
}
2626
list.push(arg.NumberValue);
2727
}
28+
for (let i = 0; i < list.length; i++) {
29+
if (list[i] < 0) {
30+
return this.parameterError(i + 1);
31+
}
32+
}
2833

2934
return Operand.Create(this.calculateGCD(list));
3035
}

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ class Function_GESTEP extends Function_N {
1111
}
1212

1313
evaluate(engine, tempParameter) {
14+
if (this.z.length < 1 || this.z.length > 2) {
15+
return this.functionError();
16+
}
1417
let args1 = this.getNumber(engine, tempParameter, 0);
1518
if (args1.IsError) { return args1; }
1619
let number = args1.NumberValue;

0 commit comments

Comments
 (0)