d3 / d3-interpolate

Interpolate numbers, colors, strings, arrays, objects, whatever!

Home Page:https://d3js.org/d3-interpolate

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Interpolate with t=Infinity returns NaN instead of Infinity

severo opened this issue · comments

d3.interpolateNumber(0,1)(Infinity)

returns NaN, while I would expect it to return Infinity.

Note that replacing

return a * (1 - t) + b * t;

with

    return (b - a) *  t + a;

would generate a new NaN issue that is not a problem with the current version:

d3.interpolateNumber(Infinity, Infinity)(0.5)

Related to #73

Two possible implementations as discussed on https://d3js.zulipchat.com/#narrow/stream/273725-D3/topic/d3-scale

isFinite(t) ? a * (1 - t) + b * t : (b - a) * t + a

and

(t * t !== Infinity) ? a * (1 - t) + b * t : (b - a) * t + a

It’s doing exactly what it’s documented to do, so I’m not sure I consider this a bug. 0 * Infinity is NaN. Why is it useful to support Infinity?

I found the case while wanting to transform a domain extent (that accepts -Infinity and Infinity as valid) to a range extent, something like:

[-Infinity, Infinity].map(d3.scaleLinear())

and I expected having [-Infinity, Infinity] as the output, but got [NaN, NaN] instead.

It's not difficult to test for this case in my application, but I was wondering if it was the "normal" behavior. If it is, OK for me to close the issue.

For the record, another possible implementation by Jacob Rus:

lerp = (a, b, t) => {
  var y = a * (1 - t) + b * t;
  return (y === y) ? y : (b - a) * t
}

It’s doing exactly what it’s documented to do, so I’m not sure I consider this a bug. 0 * Infinity is NaN. Why is it useful to support Infinity?

This scale is a way of defining a certain linear function f. In the event that the slope is not 0, f(∞) should theoretically be ±∞, not undefined.

(Whether it is important or useful to support this edge case is debatable.)