Skip to content

Commit a7908f0

Browse files
author
David T Wilcox
committedDec 21, 2022
Revised average
Revised average to use moving window formula. Mitigates large sum issues with the traditional average formula.
1 parent 59dea27 commit a7908f0

File tree

1 file changed

+7
-3
lines changed

1 file changed

+7
-3
lines changed
 

‎src/library/stats_native.c

+7-3
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,14 @@ void defineStatsFunctions() {
55
}
66

77
Value averageNative(int argCount, Value* args) {
8-
double sum = 0.0;
8+
double mean = 0.0;
9+
int count = 1;
910
for (int i = 0; i < argCount; i++) {
10-
sum += AS_NUMBER(args[i]);
11+
double value = AS_NUMBER(args[i]);
12+
double diff = (value - mean) / count;
13+
mean = mean + diff;
14+
count++;
1115
}
1216

13-
return NUMBER_VAL(sum / (double)argCount);
17+
return NUMBER_VAL(mean);
1418
}

0 commit comments

Comments
 (0)
Please sign in to comment.