From e589723baaffc2da6082925e604f11362abf21f7 Mon Sep 17 00:00:00 2001 From: Chris Duncan Date: Wed, 8 Jan 2025 14:22:48 -0800 Subject: [PATCH] Improve geometric mean by using log approach. --- test/GLOBALS.mjs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/test/GLOBALS.mjs b/test/GLOBALS.mjs index c6fab27..8728f5e 100644 --- a/test/GLOBALS.mjs +++ b/test/GLOBALS.mjs @@ -17,11 +17,11 @@ if (globalThis.sessionStorage == null) { } export function average (times) { - let sum = 0, reciprocals = 0, product = 1, count = times.length, min = 0xffff, max = 0 + let sum = 0, reciprocals = 0, logarithms = 0, count = times.length, min = 0xffff, max = 0 for (let i = 0; i < count; i++) { sum += times[i] reciprocals += 1 / times[i] - product *= times[i] + logarithms += Math.log(times[i]) min = Math.min(min, times[i]) max = Math.max(max, times[i]) } @@ -29,7 +29,7 @@ export function average (times) { total: sum, arithmetic: sum / count, harmonic: count / reciprocals, - geometric: Math.pow(product, 1 / count), + geometric: Math.exp(logarithms / count), min: min, max: max } -- 2.34.1