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
5 changes: 3 additions & 2 deletions src/bin.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,14 +87,15 @@ export default function bin() {
if (step > 0) {
for (i = 0; i < n; ++i) {
if ((x = values[i]) != null && x0 <= x && x <= x1) {
bins[Math.min(m, Math.floor((x - x0) / step))].push(data[i]);
const j = Math.floor((x - x0) / step);
bins[Math.min(m, j + (tz[j] <= x) - (tz[j - 1] > x))].push(data[i]);
}
}
} else if (step < 0) {
for (i = 0; i < n; ++i) {
if ((x = values[i]) != null && x0 <= x && x <= x1) {
const j = Math.floor((x0 - x) * step);
bins[Math.min(m, j + (tz[j] <= x))].push(data[i]); // handle off-by-one due to rounding
bins[Math.min(m, j + (tz[j] <= x) - (tz[j - 1] > x))].push(data[i]);
}
}
}
Expand Down
12 changes: 12 additions & 0 deletions test/bin-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,18 @@ it("bin(data) assigns floating point values to the correct bins", () => {
}
});

it("bin(data) assigns values just below an integer threshold to the preceding bin", () => {
const b = bin().domain([-10, 10]).thresholds(10);
assert.deepStrictEqual(b([1.9999999999999998, 2]).slice(5, 7), [box([1.9999999999999998], 0, 2), box([2], 2, 4)]);
assert.deepStrictEqual(b([-Number.MIN_VALUE, 0]).slice(4, 6), [box([-Number.MIN_VALUE], -2, 0), box([0], 0, 2)]);
});

it("bin(data) assigns values just below a fractional threshold to the preceding bin", () => {
const b = bin().domain([0, 1]).thresholds(10);
assert.deepStrictEqual(b([0.8999999999999999, 0.9]).slice(8), [box([0.8999999999999999], 0.8, 0.9), box([0.9], 0.9, 1)]);
assert.deepStrictEqual(bin().domain([-1, 0]).thresholds(10)([-0.30000000000000004, -0.3]).slice(6, 8), [box([-0.30000000000000004], -0.4, -0.3), box([-0.3], -0.3, -0.2)]);
});

it("bin(data) assigns integer values to the correct bins", () => {
assert.deepStrictEqual(bin().domain([4, 5])([5]), [box([5], 4, 5)]);
const eights = [8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8];
Expand Down
Loading