Skip to content

Commit 149d174

Browse files
committed
Fix NaN in clamped log scale inversion.
1 parent 5bae5f4 commit 149d174

4 files changed

Lines changed: 18 additions & 8 deletions

File tree

d3.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2578,11 +2578,11 @@ function d3_scale_log(linear, log) {
25782578
var d3_scale_logFormat = d3.format(".0e");
25792579

25802580
function d3_scale_logp(x) {
2581-
return Math.log(x) / Math.LN10;
2581+
return Math.log(x < 0 ? 0 : x) / Math.LN10;
25822582
}
25832583

25842584
function d3_scale_logn(x) {
2585-
return -Math.log(-x) / Math.LN10;
2585+
return -Math.log(x > 0 ? 0 : -x) / Math.LN10;
25862586
}
25872587

25882588
d3_scale_logp.pow = function(x) {

d3.min.js

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/scale/log.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,11 +69,11 @@ function d3_scale_log(linear, log) {
6969
var d3_scale_logFormat = d3.format(".0e");
7070

7171
function d3_scale_logp(x) {
72-
return Math.log(x) / Math.LN10;
72+
return Math.log(x < 0 ? 0 : x) / Math.LN10;
7373
}
7474

7575
function d3_scale_logn(x) {
76-
return -Math.log(-x) / Math.LN10;
76+
return -Math.log(x > 0 ? 0 : -x) / Math.LN10;
7777
}
7878

7979
d3_scale_logp.pow = function(x) {

test/scale/log-test.js

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,13 +106,23 @@ suite.addBatch({
106106
},
107107
"can clamp to the domain": function(log) {
108108
var x = log().clamp(true);
109-
assert.inDelta(x(.5), 0, 1e-6);
109+
assert.inDelta(x(-1), 0, 1e-6);
110110
assert.inDelta(x(5), 0.69897, 1e-6);
111111
assert.inDelta(x(15), 1, 1e-6);
112112
var x = log().domain([10, 1]).clamp(true);
113-
assert.inDelta(x(.5), 1, 1e-6);
113+
assert.inDelta(x(-1), 1, 1e-6);
114114
assert.inDelta(x(5), 0.30103, 1e-6);
115115
assert.inDelta(x(15), 0, 1e-6);
116+
},
117+
"can clamp to the range": function(log) {
118+
var x = log().clamp(true);
119+
assert.inDelta(x.invert(-.1), 1, 1e-6);
120+
assert.inDelta(x.invert(0.69897), 5, 1e-6);
121+
assert.inDelta(x.invert(1.5), 10, 1e-6);
122+
var x = log().domain([10, 1]).clamp(true);
123+
assert.inDelta(x.invert(-.1), 10, 1e-6);
124+
assert.inDelta(x.invert(0.30103), 5, 1e-6);
125+
assert.inDelta(x.invert(1.5), 1, 1e-6);
116126
}
117127
},
118128

0 commit comments

Comments
 (0)