Commit 5761a27a authored by GoshaZotov's avatar GoshaZotov

modify functions: cPERCENTILE, cRANK, cRANK_AVG

parent d2b5f8d9
...@@ -2435,22 +2435,21 @@ parserHelp.setDigitSeparator(AscCommon.g_oDefaultCultureInfo.NumberDecimalSepara ...@@ -2435,22 +2435,21 @@ parserHelp.setDigitSeparator(AscCommon.g_oDefaultCultureInfo.NumberDecimalSepara
for(var i = 0; i < args.length; i++){ for(var i = 0; i < args.length; i++){
var arg = args[i]; var arg = args[i];
if (cElementType.cellsRange === arg.type || cElementType.cellsRange3D === arg.type) { //для массивов отдельная ветка
if(typeArray && cElementType.array === typeArray[i]){ if(typeArray && cElementType.array === typeArray[i])
newArgs[i] = arg; {
}else{ if (cElementType.cellsRange === arg.type || cElementType.array === arg.type) {
newArgs[i] = arg.cross(arg1); newArgs[i] = arg.getMatrix();
} else if (cElementType.cellsRange3D === arg.type) {
newArgs[i] = arg.getMatrix()[0];
} else {
newArgs[i] = new cError(cErrorType.not_available);
} }
}else if (cElementType.cellsRange === arg.type || cElementType.cellsRange3D === arg.type) {
newArgs[i] = arg.cross(arg1);
}else if(cElementType.array === arg.type){ }else if(cElementType.array === arg.type){
if(bAddFirstArrElem){ if(bAddFirstArrElem){
if(typeArray && cElementType.array === typeArray[i]){ newArgs[i] = arg.getElementRowCol(0,0);
newArgs[i] = [];
arg.foreach(function (elem) {
newArgs[i].push(elem);
});
}else{
newArgs[i] = arg.getElementRowCol(0,0);
}
}else{ }else{
indexArr = i; indexArr = i;
newArgs[i] = arg; newArgs[i] = arg;
......
...@@ -908,7 +908,7 @@ ...@@ -908,7 +908,7 @@
{ {
//sort array //sort array
aSortArray.sort (function sortArr(a, b) { aSortArray.sort (function sortArr(a, b) {
return a.value - b.value; return a - b;
}); });
var nSize = aSortArray.length; var nSize = aSortArray.length;
...@@ -917,7 +917,7 @@ ...@@ -917,7 +917,7 @@
if ( nSize == 0 /*|| nGlobalError != FormulaError::NONE*/ ){ if ( nSize == 0 /*|| nGlobalError != FormulaError::NONE*/ ){
res = null; res = null;
} else { } else {
if ( fVal < aSortArray[ 0 ].value || fVal > aSortArray[ nSize - 1 ].value ){ if ( fVal < aSortArray[ 0 ] || fVal > aSortArray[ nSize - 1 ] ){
res = null; res = null;
}else{ }else{
var fLastPos = 0; var fLastPos = 0;
...@@ -925,12 +925,12 @@ ...@@ -925,12 +925,12 @@
var bFinished = false; var bFinished = false;
var i; var i;
for ( i = 0; i < nSize && !bFinished /*&& nGlobalError == FormulaError::NONE*/; i++ ){ for ( i = 0; i < nSize && !bFinished /*&& nGlobalError == FormulaError::NONE*/; i++ ){
if ( aSortArray[ i ].value === fVal ){ if ( aSortArray[ i ] === fVal ){
if ( fFirstPos < 0 ){ if ( fFirstPos < 0 ){
fFirstPos = i + 1.0; fFirstPos = i + 1.0;
} }
}else{ }else{
if ( aSortArray[ i ].value > fVal ){ if ( aSortArray[ i ] > fVal ){
fLastPos = i; fLastPos = i;
bFinished = true; bFinished = true;
} }
...@@ -5019,10 +5019,9 @@ ...@@ -5019,10 +5019,9 @@
cPERCENTILE.prototype.argumentsMax = 2; cPERCENTILE.prototype.argumentsMax = 2;
cPERCENTILE.prototype.numFormat = AscCommonExcel.cNumFormatNone; cPERCENTILE.prototype.numFormat = AscCommonExcel.cNumFormatNone;
cPERCENTILE.prototype.Calculate = function (arg) { cPERCENTILE.prototype.Calculate = function (arg) {
function percentile(argArray) {
function percentile(A, k) { var tA = [], A = argArray[0], alpha = argArray[1];
var tA = [], alpha = k.getValue();
for (var i = 0; i < A.length; i++) { for (var i = 0; i < A.length; i++) {
for (var j = 0; j < A[i].length; j++) { for (var j = 0; j < A[i].length; j++) {
...@@ -5039,30 +5038,17 @@ ...@@ -5039,30 +5038,17 @@
return getPercentile(tA, alpha); return getPercentile(tA, alpha);
} }
var arg0 = arg[0], arg1 = arg[1]; var oArguments = this._prepareArguments(arg, arguments[1], true, [cElementType.array]);
if (arg0 instanceof cArea || arg0 instanceof cArray) { var argClone = oArguments.args;
arg0 = arg0.getMatrix();
} else if (arg0 instanceof cArea3D) {
arg0 = arg0.getMatrix()[0];
} else {
return this.value = new cError(cErrorType.not_available);
}
if (arg1 instanceof cArea || arg1 instanceof cArea3D) {
arg1 = arg1.cross(arguments[1]);
} else if (arg1 instanceof cArray) {
arg1 = arg1.getElement(0);
}
arg1 = arg1.tocNumber(); argClone[1] = argClone[1].tocNumber();
if (arg1 instanceof cError) { var argError;
return this.value = arg1; if (argError = this._checkErrorArg(argClone)) {
return this.value = argError;
} }
return this.value = percentile(arg0, arg1); return this.value = this._findArrayInNumberArguments(oArguments, percentile);
}; };
/** /**
...@@ -5575,26 +5561,33 @@ ...@@ -5575,26 +5561,33 @@
return this.value = argError; return this.value = argError;
} }
var calcTDist = function(argArray){ var calcRank = function(argArray){
var number = argArray[0]; var number = argArray[0];
var ref = argArray[1]; var ref = argArray[1];
var order = argArray[2]; var order = argArray[2];
if(!ref.length){
return new cError(cErrorType.wrong_value_type);
}
var changedRef = []; var changedRef = [];
for(var i = 0; i < ref.length; i++){ for (var i = 0; i < ref.length; i++) {
if(cElementType.number === ref[i].type){ for (var j = 0; j < ref[i].length; j++) {
changedRef.push(ref[i]); if (ref[i][j] instanceof cError) {
return ref[i][j];
} else if (ref[i][j] instanceof cNumber) {
changedRef.push(ref[i][j].getValue());
} else if (ref[i][j] instanceof cBool) {
changedRef.push(ref[i][j].tocNumber().getValue());
}
} }
} }
if(!changedRef.length){
return new cError(cErrorType.wrong_value_type);
}
var res = rank(number, changedRef, order); var res = rank(number, changedRef, order);
return null !== res && !isNaN(res) ? new cNumber(res) : new cError(cErrorType.wrong_value_type); return null !== res && !isNaN(res) ? new cNumber(res) : new cError(cErrorType.wrong_value_type);
}; };
return this.value = this._findArrayInNumberArguments(oArguments, calcTDist); return this.value = this._findArrayInNumberArguments(oArguments, calcRank);
}; };
/** /**
...@@ -5623,26 +5616,33 @@ ...@@ -5623,26 +5616,33 @@
return this.value = argError; return this.value = argError;
} }
var calcTDist = function(argArray){ var calcRank = function(argArray){
var number = argArray[0]; var number = argArray[0];
var ref = argArray[1]; var ref = argArray[1];
var order = argArray[2]; var order = argArray[2];
if(!ref.length){
return new cError(cErrorType.wrong_value_type);
}
var changedRef = []; var changedRef = [];
for(var i = 0; i < ref.length; i++){ for (var i = 0; i < ref.length; i++) {
if(cElementType.number === ref[i].type){ for (var j = 0; j < ref[i].length; j++) {
changedRef.push(ref[i]); if (ref[i][j] instanceof cError) {
return ref[i][j];
} else if (ref[i][j] instanceof cNumber) {
changedRef.push(ref[i][j].getValue());
} else if (ref[i][j] instanceof cBool) {
changedRef.push(ref[i][j].tocNumber().getValue());
}
} }
} }
if(!changedRef.length){
return new cError(cErrorType.wrong_value_type);
}
var res = rank(number, changedRef, order, true); var res = rank(number, changedRef, order, true);
return null !== res && !isNaN(res) ? new cNumber(res) : new cError(cErrorType.wrong_value_type); return null !== res && !isNaN(res) ? new cNumber(res) : new cError(cErrorType.wrong_value_type);
}; };
return this.value = this._findArrayInNumberArguments(oArguments, calcTDist); return this.value = this._findArrayInNumberArguments(oArguments, calcRank);
}; };
/** /**
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment