Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Interpolate with t=Infinity returns NaN instead of Infinity #91

Open
severo opened this issue Jan 18, 2021 · 6 comments
Open

Interpolate with t=Infinity returns NaN instead of Infinity #91

severo opened this issue Jan 18, 2021 · 6 comments

Comments

@severo
Copy link

severo commented Jan 18, 2021

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)
@severo
Copy link
Author

severo commented Jan 18, 2021

Related to #73

@severo
Copy link
Author

severo commented Jan 18, 2021

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

@mbostock
Copy link
Member

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?

@severo
Copy link
Author

severo commented Jan 18, 2021

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.

@severo
Copy link
Author

severo commented Jan 19, 2021

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
}

@jrus
Copy link

jrus commented Jan 19, 2021

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.)

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Development

No branches or pull requests

3 participants