Hacker News new | ask | show | jobs
by Fredkin 623 days ago
Math.sin(x)/x (the sinc function) for 7 terms over [-3,3] gives coefficients c0...c6 that are all NaNs. Is this a bug?

To work around it, I handled the x near zero case by just forcing to 1.0.

if(Math.abs(x) > 1e-8 ){ Math.sin(x)/x } else { 1.0 }

3 comments

> Math.sin(x)/x (the sinc function) for 7 terms over [-3,3] gives coefficients c0...c6 that are all NaNs. Is this a bug?

That wouldn't exactly be a bug. The code is undoubtedly calculating the Chebyshev coefficients by evaluating the function on something like x_j = (xmin) + (xmax - xmin)/2(1 + cos(pi[0..j-1]/(j-1)). If one of those grid points happens to be exactly 0, it will try to evaluate Math.sin(0)/0, giving the NaN.

Another workaround is to have a slightly asymmetric range, such as [-3,+3.0000001]

The problem here likely is, that your first expression was not well defined for x=0 and seemingly the poor approximation code stumbled over it. Shame on it!
Yes, that is a bug, good catch. The app should show an error if the function is not defined in all chebyshev nodes. Like you have already discovered, it's easy to work around this issue for now.