Improve geometric mean by using log approach.
authorChris Duncan <chris@zoso.dev>
Wed, 8 Jan 2025 22:22:48 +0000 (14:22 -0800)
committerChris Duncan <chris@zoso.dev>
Wed, 8 Jan 2025 22:22:48 +0000 (14:22 -0800)
test/GLOBALS.mjs

index c6fab27732eb2e5a6af4dd1ad69e3a4dce5db1c4..8728f5eff0774535d258e22c6257fa0f3047b9fb 100644 (file)
@@ -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
        }