计算类方法

average(code,start,end) {//平均数
  let arr = []
  let total = 0
  for(let i =start; i<=end;i++) {
    arr.push(this[code+i]?this[code+i]:0)
  }
  for(let i=0;i<arr.length;i++){
    total+= arr[i]
  }
  return total/arr.length
}

 

sqrt(initNum,saveNum) {//所需开根的数字/保留的小数位
  let result = initNum >= 1 ? initNum : 1;
  while(result * result - initNum > 1e-8)
  result = 0.5 * (result + initNum / result);
  result=result.toFixed(saveNum);
  return result;
}


stdevp(code,start,end){
  let arr = []
  let total = 0//数字总数
  let stdEvpNum = 0//标准差
  for(let i =start; i<=end;i++) {
    arr.push(this[code+i]?this[code+i]:0)
  }
  for(let i=0;i<arr.length;i++){
    total+= arr[i]
  }
  for(let i=0;i<arr.length;i++){
    stdEvpNum += Math.pow((arr[i]-(total/arr.length)),2) / arr.length
  }
  return this.sqrt(stdEvpNum,2)
}


average(code,start,end) {//平均数
  let arr = []
  let total = 0//数字总数
  for(let i=start;i<=end;i++) {
    arr.push(this[code+i]?this[code+i]:0)
  }
  for(let i=0;i<arr.length;i++){
    total+= arr[i]
  }
  return total / arr.length
}


maxNum(code,start,end){//最大值
  let arr = []
  for(let i =start; i<=end;i++) {
    arr.push(this[code+i]?this[code+i]:0)
  }
  return Math.max(...arr)
}

posted @ 2020-07-13 15:00  孑孓_0209  阅读(123)  评论(0)    收藏  举报