Skip to content
Closed
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
8 changes: 8 additions & 0 deletions src/quantile.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ export default function quantile(values, p, valueof) {
i0 = Math.floor(i),
value0 = max(quickselect(values, i0).subarray(0, i0 + 1)),
value1 = min(values.subarray(i0 + 1));
if (Number.isFinite(value0) && Number.isFinite(value1) && !Number.isFinite(value1 - value0)) {
const t = i - i0;
return value0 * (1 - t) + value1 * t;
}
return value0 + (value1 - value0) * (i - i0);
}

Expand All @@ -29,6 +33,10 @@ export function quantileSorted(values, p, valueof = number) {
i0 = Math.floor(i),
value0 = +valueof(values[i0], i0, values),
value1 = +valueof(values[i0 + 1], i0 + 1, values);
if (Number.isFinite(value0) && Number.isFinite(value1) && !Number.isFinite(value1 - value0)) {
const t = i - i0;
return value0 * (1 - t) + value1 * t;
}
return value0 + (value1 - value0) * (i - i0);
}

Expand Down
12 changes: 12 additions & 0 deletions test/median-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,18 @@ it("median(array) can handle large numbers without overflowing", () => {
assert.strictEqual(median([-Number.MAX_VALUE, -Number.MAX_VALUE]), -Number.MAX_VALUE);
});

it("median(array) handles large opposite-sign values without overflowing", () => {
const h = 2 ** 1023;
assert.strictEqual(median([-1e308, 1e308]), 0);
assert.strictEqual(median([-1.5 * h, h]), -0.25 * h);
assert.strictEqual(median([-h, 1.5 * h]), 0.25 * h);
});

it("median(array) returns the selected finite value at an integer position", () => {
const h = 2 ** 1023;
assert.strictEqual(median([-h, -h, h]), -h);
});

it("median(array) returns undefined if the array contains no observed values", () => {
assert.strictEqual(median([]), undefined);
assert.strictEqual(median([null]), undefined);
Expand Down
25 changes: 25 additions & 0 deletions test/quantile-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,31 @@ it("quantile(array, p) uses the R-7 method", () => {
assert.strictEqual(quantile(odd, 1), 20);
});

it("quantile and quantileSorted interpolate large opposite-sign values without overflowing", () => {
for (const q of [quantile, quantileSorted]) {
assert.strictEqual(q([-1e308, 1e308], 0.5), 0);
}
});

it("quantile and quantileSorted interpolate large asymmetric opposite-sign values", () => {
const h = 2 ** 1023;
for (const q of [quantile, quantileSorted]) {
assert.strictEqual(q([-1.5 * h, h], 0.25), -0.875 * h);
assert.strictEqual(q([-1.5 * h, h], 0.5), -0.25 * h);
assert.strictEqual(q([-1.5 * h, h], 0.75), 0.375 * h);
assert.strictEqual(q([-h, 1.5 * h], 0.25), -0.375 * h);
assert.strictEqual(q([-h, 1.5 * h], 0.5), 0.25 * h);
assert.strictEqual(q([-h, 1.5 * h], 0.75), 0.875 * h);
}
});

it("quantile and quantileSorted return the selected finite value at an integer position", () => {
const h = 2 ** 1023;
for (const q of [quantile, quantileSorted]) {
assert.strictEqual(q([-h, -h, h], 0.5), -h);
}
});

it("quantile(array, p) coerces values to numbers", () => {
const strings = ["1", "2", "3", "4"];
assert.strictEqual(quantile(strings, 1 / 3), 2);
Expand Down