Skip to content

Commit

Permalink
Fix NaN in clamped log scale inversion.
Browse files Browse the repository at this point in the history
  • Loading branch information
mbostock committed Jan 1, 2012
1 parent 5bae5f4 commit 149d174
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 8 deletions.
4 changes: 2 additions & 2 deletions d3.js
Original file line number Diff line number Diff line change
Expand Up @@ -2578,11 +2578,11 @@ function d3_scale_log(linear, log) {
var d3_scale_logFormat = d3.format(".0e");

function d3_scale_logp(x) {
return Math.log(x) / Math.LN10;
return Math.log(x < 0 ? 0 : x) / Math.LN10;
}

function d3_scale_logn(x) {
return -Math.log(-x) / Math.LN10;
return -Math.log(x > 0 ? 0 : -x) / Math.LN10;
}

d3_scale_logp.pow = function(x) {
Expand Down
4 changes: 2 additions & 2 deletions d3.min.js

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions src/scale/log.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,11 @@ function d3_scale_log(linear, log) {
var d3_scale_logFormat = d3.format(".0e");

function d3_scale_logp(x) {
return Math.log(x) / Math.LN10;
return Math.log(x < 0 ? 0 : x) / Math.LN10;
}

function d3_scale_logn(x) {
return -Math.log(-x) / Math.LN10;
return -Math.log(x > 0 ? 0 : -x) / Math.LN10;
}

d3_scale_logp.pow = function(x) {
Expand Down
14 changes: 12 additions & 2 deletions test/scale/log-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,13 +106,23 @@ suite.addBatch({
},
"can clamp to the domain": function(log) {
var x = log().clamp(true);
assert.inDelta(x(.5), 0, 1e-6);
assert.inDelta(x(-1), 0, 1e-6);
assert.inDelta(x(5), 0.69897, 1e-6);
assert.inDelta(x(15), 1, 1e-6);
var x = log().domain([10, 1]).clamp(true);
assert.inDelta(x(.5), 1, 1e-6);
assert.inDelta(x(-1), 1, 1e-6);
assert.inDelta(x(5), 0.30103, 1e-6);
assert.inDelta(x(15), 0, 1e-6);
},
"can clamp to the range": function(log) {
var x = log().clamp(true);
assert.inDelta(x.invert(-.1), 1, 1e-6);
assert.inDelta(x.invert(0.69897), 5, 1e-6);
assert.inDelta(x.invert(1.5), 10, 1e-6);
var x = log().domain([10, 1]).clamp(true);
assert.inDelta(x.invert(-.1), 10, 1e-6);
assert.inDelta(x.invert(0.30103), 5, 1e-6);
assert.inDelta(x.invert(1.5), 1, 1e-6);
}
},

Expand Down

0 comments on commit 149d174

Please sign in to comment.