You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
* @throws {TypeError} must provide a number primitive
32
72
* @returns {Function} accumulator function
33
73
*
34
74
* @example
35
-
* var accumulator = incrnancv();
75
+
* var accumulator = incrcv();
36
76
*
37
77
* var cv = accumulator();
38
78
* // returns null
39
79
*
40
-
* cv = accumulator(2.0);
41
-
* // returns 0.0
42
-
*
43
-
* cv = accumulator(NaN);
80
+
* cv = accumulator( 2.0 );
44
81
* // returns 0.0
45
82
*
46
-
* cv = accumulator(1.0);
83
+
* cv = accumulator(1.0);
47
84
* // returns ~0.47
48
85
*
49
86
* cv = accumulator();
50
87
* // returns ~0.47
88
+
*
89
+
* @example
90
+
* var accumulator = incrcv( 3.14 );
51
91
*/
52
-
functionincrnancv(){
53
-
varcv=incrcv();
54
-
functionaccumulator(x){
55
-
if(arguments.length===0){
56
-
returncv();
92
+
functionincrcv(mean){
93
+
vardelta;
94
+
varmu;
95
+
varM2;
96
+
varN;
97
+
98
+
M2=0.0;
99
+
N=0;
100
+
if(arguments.length){
101
+
if(!isNumber(mean)){
102
+
thrownewTypeError(format('invalid argument. Must provide a number. Value: `%s`.',mean));
57
103
}
58
-
if(isnan(x)){
59
-
returncv();
104
+
mu=mean;
105
+
returnaccumulator2;
106
+
}
107
+
mu=0.0;
108
+
returnaccumulator1;
109
+
110
+
/**
111
+
* If provided a value, the accumulator function returns an updated accumulated value. If not provided a value, the accumulator function returns the current accumulated value.
112
+
*
113
+
* @private
114
+
* @param {number} [x] - new value
115
+
* @returns {(number|null)} accumulated value or null
116
+
*/
117
+
functionaccumulator1(x){
118
+
if(arguments.length===0){
119
+
if(N===0){
120
+
returnnull;
121
+
}
122
+
if(N===1){
123
+
return(isnan(M2)) ? NaN : 0.0/mu;
124
+
}
125
+
returnsqrt(M2/(N-1))/mu;
126
+
}
127
+
N+=1;
128
+
delta=x-mu;
129
+
mu+=delta/N;
130
+
M2+=delta*(x-mu);
131
+
if(N<2){
132
+
return(isnan(M2)) ? NaN : 0.0/mu;
60
133
}
61
-
returncv(x);
134
+
returnsqrt(M2/(N-1))/mu;
62
135
}
63
136
64
-
returnaccumulator;
137
+
/**
138
+
* If provided a value, the accumulator function returns an updated accumulated value. If not provided a value, the accumulator function returns the current accumulated value.
139
+
*
140
+
* @private
141
+
* @param {number} [x] - new value
142
+
* @returns {(number|null)} accumulated value or null
0 commit comments