Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions src/gmean.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import {Adder} from "./fsum.js";
import {numbers} from "./number.js";

export default function gmean(values, valueof) {
const logs = new Adder();
let count = 0, sign = 0, min = Infinity, max = 0;
for (const value of numbers(values, valueof)) {
const magnitude = Math.abs(value);
const direction = Math.sign(value);
if (direction && sign && direction !== sign) return NaN;
if (direction) sign = direction;
if (magnitude < min) min = magnitude;
if (magnitude > max) max = magnitude;
if (magnitude > 0 && magnitude < Infinity) logs.add(Math.log2(magnitude));
++count;
}
if (!count) return;
if (min === 0) return max === Infinity ? NaN : 0;
if (min === max || max === Infinity) return sign * max;
// Rounding the logarithm of a large finite double can yield an exponent of 1024.
return sign * Math.max(min, Math.min(max, 2 ** (+logs / count)));
}
1 change: 1 addition & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export {default as thresholdSturges} from "./threshold/sturges.js";
export {default as max} from "./max.js";
export {default as maxIndex} from "./maxIndex.js";
export {default as mean} from "./mean.js";
export {default as gmean} from "./gmean.js";
export {default as median, medianIndex} from "./median.js";
export {default as merge} from "./merge.js";
export {default as min} from "./min.js";
Expand Down
40 changes: 40 additions & 0 deletions test/gmean-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import assert from "node:assert";
import {it} from "vitest";
import {gmean} from "d3-array";
import {OneTimeNumber} from "./OneTimeNumber.js";

it("gmean returns the geometric mean without overflowing or underflowing the product", () => {
assert.strictEqual(gmean([2 ** 1000, 2 ** 1002]), 2 ** 1001);
assert.strictEqual(gmean([2 ** -1000, 2 ** -1002]), 2 ** -1001);
assert.strictEqual(gmean([1e300, 1e-300]), 1);
assert.strictEqual(gmean([Number.MAX_VALUE, Number.MAX_VALUE]), Number.MAX_VALUE);
assert.strictEqual(gmean([Number.MIN_VALUE]), Number.MIN_VALUE);
});

it("gmean accepts iterables, ignores missing values, and coerces each value once", () => {
assert.strictEqual(gmean(new Set([null, undefined, NaN, "2", new OneTimeNumber(8)])), 4);
assert.strictEqual(gmean([]), undefined);
assert.strictEqual(gmean([null, NaN]), undefined);
});

it("gmean passes the value, index and original iterable to the accessor", () => {
const values = new Set(["first", "last"]);
assert.strictEqual(gmean(values, (value, i, source) => {
assert.strictEqual(source, values);
assert.strictEqual(value, ["first", "last"][i]);
return [1, 9][i];
}), 3);
});

it("gmean preserves a common sign and is undefined over mixed signs", () => {
assert.strictEqual(gmean([-1, -4]), -2);
assert.ok(Number.isNaN(gmean([-1, 4])));
assert.ok(Number.isNaN(gmean([4, -1])));
});

it("gmean handles zeros and infinities", () => {
assert.strictEqual(gmean([0, 4]), 0);
assert.strictEqual(gmean([-4, 0]), 0);
assert.strictEqual(gmean([2, Infinity]), Infinity);
assert.ok(Number.isNaN(gmean([0, Infinity])));
});