summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorsanine <sanine.not@pm.me>2022-05-25 22:22:41 -0500
committersanine <sanine.not@pm.me>2022-05-25 22:22:41 -0500
commite2799cb092359038374cde25eb691d667b309383 (patch)
tree4d02e5f477ad2264e5c4a0d9e7d37f59d3fe0882
parent5a87004c6e1ee2a0731269919c07ca211ebfd099 (diff)
add Util.js
-rw-r--r--modules/Util.js13
-rw-r--r--modules/Util.test.js23
2 files changed, 36 insertions, 0 deletions
diff --git a/modules/Util.js b/modules/Util.js
new file mode 100644
index 0000000..cbe466d
--- /dev/null
+++ b/modules/Util.js
@@ -0,0 +1,13 @@
+function useAverage() {
+ var avg = 0;
+ let weight = 0;
+ const append = value => {
+ avg = (weight * avg) + value;
+ weight += 1;
+ avg = avg/weight;
+ }
+
+ return [() => avg, append];
+}
+
+export { useAverage };
diff --git a/modules/Util.test.js b/modules/Util.test.js
new file mode 100644
index 0000000..000d54e
--- /dev/null
+++ b/modules/Util.test.js
@@ -0,0 +1,23 @@
+import { test, assert} from './test-assert.js';
+import { useAverage } from './Util.js';
+
+
+test('Average correctly accumulates an average', () => {
+ let [avg, avg_append] = useAverage();
+ let data = [];
+ for (let i=0; i<5000; i++) {
+ let d = Math.random();
+ data.push(d);
+ avg_append(d);
+ }
+
+ let manual_average = 0;
+ for (let d of data) manual_average += d;
+ manual_average /= data.length;
+
+ const precision = (decimalPlaces, num) => {
+ const theta = 10**decimalPlaces;
+ return Math.floor(num * theta) / theta;
+ };
+ assert.equal(precision(5, avg()), precision(5, manual_average));
+});