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
2 changes: 2 additions & 0 deletions src/nice.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import {tickIncrement} from "./ticks.js";

export default function nice(start, stop, count) {
// An interval crossing zero needs at least two ticks to converge.
if (start < 0 && stop > 0 && count > 0 && count < 2) count = 2;
let prestep;
while (true) {
const step = tickIncrement(start, stop, count);
Expand Down
10 changes: 10 additions & 0 deletions test/bin-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -267,3 +267,13 @@ function box(bin, x0, x1) {
function* iterable(array) {
yield* array;
}


it("bin() keeps finite bounds when one threshold spans zero", () => {
const bins = bin().thresholds(1)([-1.4, 5.6]);
assert.strictEqual(bins.flat().length, 2);
for (const {x0, x1} of bins) {
assert(Number.isFinite(x0));
assert(Number.isFinite(x1));
}
});
11 changes: 11 additions & 0 deletions test/nice-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,3 +61,14 @@ it("nice(start, stop, count) handles large steps precisely", () => {
assert.deepStrictEqual(nice(0.929999999999999e10, 1.07e10, 5000), [0.92998e10, 1.07e10]);
assert.deepStrictEqual(nice(-1.07e10, -0.929999999999999e10, 5000), [-1.07e10, -0.92998e10]);
});


it("nice(start, stop, count) keeps intervals crossing zero finite for small counts", () => {
for (const count of [0.5, 1, 1.5]) {
for (const [start, stop] of [[-1, 5], [-0.1, 0.2], [-5, 1], [-1, 1]]) {
const [a, b] = nice(start, stop, count);
assert(a <= start && b >= stop);
assert(b - a < 100 * (stop - start));
}
}
});