Last change
on this file since 20061 was 18225, checked in by dneise, 9 years ago |
adding new library Func.js, was formerly implemented inside CheckUnderflow.js
|
File size:
1.2 KB
|
Line | |
---|
1 | 'use strict';
|
---|
2 |
|
---|
3 | if (!("Func" in this))
|
---|
4 | {
|
---|
5 | var Func = {
|
---|
6 |
|
---|
7 | sum : function(a, b) {
|
---|
8 | return a + b;
|
---|
9 | },
|
---|
10 | sq : function(a, b) {
|
---|
11 | return Math.sqrt(a*a + b*b);
|
---|
12 | },
|
---|
13 | min : function(a, b) {
|
---|
14 | return Math.min(a, b);
|
---|
15 | },
|
---|
16 | max : function(a, b) {
|
---|
17 | return Math.max(a, b);
|
---|
18 | },
|
---|
19 | avg : function(arr) {
|
---|
20 | return arr.reduce(this.Sum, 0)/arr.length;
|
---|
21 | }
|
---|
22 | stat : function(arr, func)
|
---|
23 | {
|
---|
24 | if (arr.length==0)
|
---|
25 | return undefined;
|
---|
26 |
|
---|
27 | var sum = 0;
|
---|
28 | var sq = 0;
|
---|
29 | var cnt = 0;
|
---|
30 | var min = arr[0];
|
---|
31 | var max = arr[0];
|
---|
32 | arr.forEach(
|
---|
33 | function(val, idx) {
|
---|
34 | sum+=val;
|
---|
35 | sq+=val*val;
|
---|
36 | if (val>max)
|
---|
37 | max=val;
|
---|
38 | if (val<min)
|
---|
39 | min=val;
|
---|
40 | if (func && func(val, idx))
|
---|
41 | cnt++
|
---|
42 | }
|
---|
43 | );
|
---|
44 | sum /= arr.length;
|
---|
45 | sq /= arr.length;
|
---|
46 |
|
---|
47 | return { avg:sum, rms:Math.sqrt(sq-sum*sum), min:min, max:max, count:cnt };
|
---|
48 | },
|
---|
49 | };
|
---|
50 | }
|
---|
51 | else{
|
---|
52 | console.out("multiple include of 'Func.js'")
|
---|
53 | }
|
---|
Note:
See
TracBrowser
for help on using the repository browser.