From 52bce28e39d35005be3ccf4640cb8bd88b588821 Mon Sep 17 00:00:00 2001 From: xufanglu <3146974+likev@users.noreply.github.com> Date: Sun, 7 Mar 2021 02:15:42 +0800 Subject: [PATCH] fixed d3.range(0, 1, 1/49) returns 50 elements! d3.range(0, 1, step) is frequently used scenario and I think we should fixed it. --- src/range.js | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/src/range.js b/src/range.js index 59756015..b054ff18 100644 --- a/src/range.js +++ b/src/range.js @@ -2,9 +2,14 @@ export default function(start, stop, step) { start = +start, stop = +stop, step = (n = arguments.length) < 2 ? (stop = start, start = 0, 1) : n < 3 ? 1 : +step; var i = -1, - n = Math.max(0, Math.ceil((stop - start) / step)) | 0, - range = new Array(n); - + n = (stop - start) / step, + floor_n = Math.floor(n); + + if(n > 0 && n - floor_n < 1e-10) n = floor_n;//so that d3.range(0, 1, 1/49) returns 49 elements! + else n = Math.max(0, Math.ceil(n)) | 0; + + var range = new Array(n); + while (++i < n) { range[i] = start + i * step; }