Skip to content

Commit

Permalink
Fix boundary condition in precisionRound.
Browse files Browse the repository at this point in the history
  • Loading branch information
mbostock committed Dec 29, 2015
1 parent 389adae commit 982ee06
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/precisionRound.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import exponent from "./exponent";

export default function(step, max) {
return Math.max(0, exponent(Math.abs(max)) - exponent(Math.abs(step))) + 1;
step = Math.abs(step), max = Math.abs(max) - step;
return Math.max(0, exponent(max) - exponent(step)) + 1;
};
10 changes: 10 additions & 0 deletions test/precisionRound-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
var tape = require("tape"),
format = require("../");

tape("precisionRound(step, max) returns the expected value", function(test) {
test.equal(format.precisionRound(0.1, 1.1), 2); // "1.0", "1.1"
test.equal(format.precisionRound(0.01, 0.99), 2); // "0.98", "0.99"
test.equal(format.precisionRound(0.01, 1.00), 2); // "0.99", "1.0"
test.equal(format.precisionRound(0.01, 1.01), 3); // "1.00", "1.01"
test.end();
});

0 comments on commit 982ee06

Please sign in to comment.