Skip to content

Commit 0d261cb

Browse files
committed
fix
1 parent 8ac6e65 commit 0d261cb

File tree

6 files changed

+30
-11
lines changed

6 files changed

+30
-11
lines changed

csharp/ToolGood.Algorithm.Fast/Internals/Functions/FunctionBase.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1039,7 +1039,7 @@ public override Operand Calculate(AlgorithmEngine work)
10391039

10401040
var vague = true;
10411041
if (args.Count == 4) {
1042-
var args4 = args[2].ToBoolean("Function VLOOKUP parameter 4 is error!");
1042+
var args4 = args[3].ToBoolean("Function VLOOKUP parameter 4 is error!");
10431043
if (args4.IsError) { return args4; }
10441044
vague = args4.BooleanValue;
10451045
}

csharp/ToolGood.Algorithm.Fast/Internals/Functions/FunctionBase.flow.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public Function_IF(FunctionBase func1, FunctionBase func2, FunctionBase func3) :
1515

1616
public override Operand Calculate(AlgorithmEngine work)
1717
{
18-
var args1 = func1.Calculate(work); if (args1.Type != OperandType.BOOLEAN) { args1 = args1.ToBoolean("Function IF first parameter is error!"); if (args1.IsError) { return args1; } }
18+
var args1 = func1.Calculate(work); if (args1.Type != OperandType.BOOLEAN) { args1 = args1.ToBoolean("Function IF first parameter must be boolean!"); if (args1.IsError) { return args1; } }
1919
if (args1.BooleanValue) return func2.Calculate(work);
2020
if (func3 == null) { return Operand.False; }
2121
return func3.Calculate(work);

csharp/ToolGood.Algorithm.Fast/Internals/Functions/FunctionBase.math.cs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,9 @@ public Function_SQRT(FunctionBase func1) : base(func1)
7777
public override Operand Calculate(AlgorithmEngine work)
7878
{
7979
var args1 = func1.Calculate(work); if (args1.Type != OperandType.NUMBER) { args1 = args1.ToNumber("Function SQRT parameter is error!"); if (args1.IsError) { return args1; } }
80+
if (args1.NumberValue < 0) {
81+
return Operand.Error("Function SQRT parameter is error!");
82+
}
8083
return Operand.Create(Math.Sqrt((double)args1.NumberValue));
8184
}
8285
public override void ToString(StringBuilder stringBuilder, bool addBrackets)
@@ -161,6 +164,9 @@ public override Operand Calculate(AlgorithmEngine work)
161164

162165
var total = args1.IntValue;
163166
var count = args2.IntValue;
167+
if (total < 0 || count < 0 || total < count) {
168+
return Operand.Error("Function COMBIN parameter is error!");
169+
}
164170
decimal sum = 1;
165171
decimal sum2 = 1;
166172
for (int i = 0; i < count; i++) {
@@ -1198,6 +1204,7 @@ public override Operand Calculate(AlgorithmEngine work)
11981204
if (o1 == false) { return Operand.Error("Function COVARIANCE.S parameter 1 error!"); }
11991205
if (o2 == false) { return Operand.Error("Function COVARIANCE.S parameter 2 error!"); }
12001206
if (list1.Count != list2.Count) { return Operand.Error("Function COVARIANCE.S parameter's count error!"); }
1207+
if (list1.Count == 1) { return Operand.Error("Function COVARIANCE.S parameter's count error!"); }
12011208

12021209
var avg1 = list1.Average();
12031210
var avg2 = list2.Average();
@@ -1231,6 +1238,7 @@ public override Operand Calculate(AlgorithmEngine work)
12311238
if (o1 == false) { return Operand.Error("Function COVAR parameter 1 error!"); }
12321239
if (o2 == false) { return Operand.Error("Function COVAR parameter 2 error!"); }
12331240
if (list1.Count != list2.Count) { return Operand.Error("Function COVAR parameter's count error!"); }
1241+
if (list1.Count == 0) { return Operand.Error("Function COVAR parameter's count error!"); }
12341242

12351243
var avg1 = list1.Average();
12361244
var avg2 = list2.Average();
@@ -1392,7 +1400,7 @@ public override Operand Calculate(AlgorithmEngine work)
13921400
int sum = 0;
13931401
int n = 1;
13941402
for (int i = 0; i < list.Count; i++) {
1395-
var a = (int)list[i];
1403+
var a = (int)list[i]; // 小于等于0 时,按0处理
13961404
n *= FunctionUtil.F_base_Factorial(a);
13971405
sum += a;
13981406
}

csharp/ToolGood.Algorithm.Fast/Internals/Functions/FunctionBase.string.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -475,6 +475,10 @@ public override Operand Calculate(AlgorithmEngine work)
475475
for (int i = 0; i < text.Length; i++) {
476476
bool b = true;
477477
for (int j = 0; j < oldtext.Length; j++) {
478+
if (i + j >= text.Length) {
479+
b = false;
480+
break;
481+
}
478482
var t = text[i + j];
479483
var t2 = oldtext[j];
480484
if (t != t2) {

csharp/ToolGood.Algorithm.Fast/Internals/Functions/FunctionBase.sum.cs

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,9 @@ public override Operand Calculate(AlgorithmEngine work)
216216
}
217217
}
218218
}
219+
if (count == 0) {
220+
return Operand.Error("Function AVERAGEIF divide by zero error!");
221+
}
219222
return Operand.Create(sum / count);
220223
}
221224
public override void ToString(StringBuilder stringBuilder, bool addBrackets)
@@ -253,7 +256,7 @@ public Function_COUNTIF(FunctionBase func1, FunctionBase func2) : base(func1, fu
253256

254257
public override Operand Calculate(AlgorithmEngine work)
255258
{
256-
var args1 = func1.Calculate(work); if (args1.Type != OperandType.ARRARY) { args1 = args1.ToNumber("Function COUNTIF parameter 1 is error!"); if (args1.IsError) { return args1; } }
259+
var args1 = func1.Calculate(work); if (args1.Type != OperandType.ARRARY) { args1 = args1.ToArray("Function COUNTIF parameter 1 is error!"); if (args1.IsError) { return args1; } }
257260
var args2 = func2.Calculate(work); if (args2.IsError) { return args2; }
258261
List<decimal> list = new List<decimal>();
259262
var o = FunctionUtil.F_base_GetList(args1, list);
@@ -346,7 +349,7 @@ public Function_LARGE(FunctionBase func1, FunctionBase func2) : base(func1, func
346349

347350
public override Operand Calculate(AlgorithmEngine work)
348351
{
349-
var args1 = func1.Calculate(work); if (args1.Type != OperandType.ARRARY) { args1 = args1.ToNumber("Function LARGE parameter 1 is error!"); if (args1.IsError) { return args1; } }
352+
var args1 = func1.Calculate(work); if (args1.Type != OperandType.ARRARY) { args1 = args1.ToArray("Function LARGE parameter 1 is error!"); if (args1.IsError) { return args1; } }
350353
var args2 = func2.Calculate(work); if (args2.Type != OperandType.NUMBER) { args2 = args2.ToNumber("Function LARGE parameter 2 is error!"); if (args2.IsError) { return args2; } }
351354
List<decimal> list = new List<decimal>();
352355
var o = FunctionUtil.F_base_GetList(args1, list);
@@ -373,7 +376,7 @@ public Function_SMALL(FunctionBase func1, FunctionBase func2) : base(func1, func
373376

374377
public override Operand Calculate(AlgorithmEngine work)
375378
{
376-
var args1 = func1.Calculate(work); if (args1.Type != OperandType.ARRARY) { args1 = args1.ToNumber("Function SMALL parameter 1 is error!"); if (args1.IsError) { return args1; } }
379+
var args1 = func1.Calculate(work); if (args1.Type != OperandType.ARRARY) { args1 = args1.ToArray("Function SMALL parameter 1 is error!"); if (args1.IsError) { return args1; } }
377380
var args2 = func2.Calculate(work); if (args2.Type != OperandType.NUMBER) { args2 = args2.ToNumber("Function SMALL parameter 2 is error!"); if (args2.IsError) { return args2; } }
378381
List<decimal> list = new List<decimal>();
379382
var o = FunctionUtil.F_base_GetList(args1, list);
@@ -399,7 +402,7 @@ public Function_PERCENTILE(FunctionBase func1, FunctionBase func2) : base(func1,
399402

400403
public override Operand Calculate(AlgorithmEngine work)
401404
{
402-
var args1 = func1.Calculate(work); if (args1.Type != OperandType.ARRARY) { args1 = args1.ToNumber("Function PERCENTILE parameter 1 is error!"); if (args1.IsError) { return args1; } }
405+
var args1 = func1.Calculate(work); if (args1.Type != OperandType.ARRARY) { args1 = args1.ToArray("Function PERCENTILE parameter 1 is error!"); if (args1.IsError) { return args1; } }
403406
var args2 = func2.Calculate(work); if (args2.Type != OperandType.NUMBER) { args2 = args2.ToNumber("Function PERCENTILE parameter 2 is error!"); if (args2.IsError) { return args2; } }
404407
List<decimal> list = new List<decimal>();
405408
var o = FunctionUtil.F_base_GetList(args1, list);
@@ -466,6 +469,9 @@ public override Operand Calculate(AlgorithmEngine work)
466469
}
467470
sum += 1 / db;
468471
}
472+
if (sum == 0) {
473+
return Operand.Error("Function HARMEAN parameter error!");
474+
}
469475
return Operand.Create(list.Count / sum);
470476
}
471477
public override void ToString(StringBuilder stringBuilder, bool addBrackets)
@@ -651,7 +657,7 @@ public override Operand Calculate(AlgorithmEngine work)
651657
List<decimal> list = new List<decimal>();
652658
var o = FunctionUtil.F_base_GetList(args, list);
653659
if (o == false) { return Operand.Error("Function VAR parameter error!"); }
654-
if (list.Count == 0) { return Operand.Error("Function VAR parameter error!"); }
660+
if (list.Count <= 1) { return Operand.Error("Function VAR parameter error!"); }
655661
decimal sum = 0;
656662
decimal sum2 = 0;
657663
for (int i = 0; i < list.Count; i++) {
@@ -683,6 +689,7 @@ public override Operand Calculate(AlgorithmEngine work)
683689
var o = FunctionUtil.F_base_GetList(args, list);
684690
if (o == false) { return Operand.Error("Function VARP parameter error!"); }
685691
if (list.Count == 0) { return Operand.Error("Function VARP parameter error!"); }
692+
if (list.Count == 1) { return Operand.Zero; }
686693

687694
decimal sum = 0;
688695
decimal avg = list.Average();
@@ -843,7 +850,7 @@ public override Operand Calculate(AlgorithmEngine work)
843850
var args1 = func1.Calculate(work); if (args1.Type != OperandType.NUMBER) { args1 = args1.ToNumber("Function BINOMDIST parameter 1 error!"); if (args1.IsError) return args1; }
844851
var args2 = func2.Calculate(work); if (args2.Type != OperandType.NUMBER) { args2 = args2.ToNumber("Function BINOMDIST parameter 2 error!"); if (args2.IsError) return args2; }
845852
var args3 = func3.Calculate(work); if (args3.Type != OperandType.NUMBER) { args3 = args3.ToNumber("Function BINOMDIST parameter 3 error!"); if (args3.IsError) return args3; }
846-
var args4 = func4.Calculate(work); if (args3.Type != OperandType.BOOLEAN) { args4 = args4.ToBoolean("Function BINOMDIST parameter 4 error!"); if (args4.IsError) return args4; }
853+
var args4 = func4.Calculate(work); if (args4.Type != OperandType.BOOLEAN) { args4 = args4.ToBoolean("Function BINOMDIST parameter 4 error!"); if (args4.IsError) return args4; }
847854

848855
if (!(args3.NumberValue >= 0.0m && args3.NumberValue <= 1.0m && args2.NumberValue >= 0)) {
849856
return Operand.Error("Function BINOMDIST parameter error!");
@@ -1246,7 +1253,7 @@ public Function_QUARTILE(FunctionBase func1, FunctionBase func2) : base(func1, f
12461253

12471254
public override Operand Calculate(AlgorithmEngine work)
12481255
{
1249-
var args1 = func1.Calculate(work); if (args1.Type != OperandType.ARRARY) { args1 = args1.ToNumber("Function QUARTILE parameter 1 is error!"); if (args1.IsError) { return args1; } }
1256+
var args1 = func1.Calculate(work); if (args1.Type != OperandType.ARRARY) { args1 = args1.ToArray("Function QUARTILE parameter 1 is error!"); if (args1.IsError) { return args1; } }
12501257
var args2 = func2.Calculate(work); if (args2.Type != OperandType.NUMBER) { args2 = args2.ToNumber("Function QUARTILE parameter 2 is error!"); if (args2.IsError) { return args2; } }
12511258

12521259
List<decimal> list = new List<decimal>();

csharp/ToolGood.Algorithm.Fast/Internals/Functions/FunctionUtil.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ public static int F_base_lgm(List<decimal> list)
194194

195195
public static int F_base_Factorial(int a)
196196
{
197-
if (a == 0) { return 1; }
197+
if (a <= 0) { return 1; }
198198
int r = 1;
199199
for (int i = a; i > 0; i--) {
200200
r *= i;

0 commit comments

Comments
 (0)