Skip to content

Numerically stable sinc function

Question

Implement the function f(x)=sin⁡(x)/xf(x)=\sin(x)/x so that it returns sensible values for every real input.

Short answer

Special-case small ∣x∣|x| with a Taylor series. At x=0x=0 the formula is 0/00/0 although the limit is 11, and very near zero the ratio loses accuracy. Below a small threshold return 1−x2/6+x4/1201-x^2/6+x^4/120; otherwise compute sin⁡(x)/x\sin(x)/x directly. The function is even, and for large ∣x∣|x| the direct formula is fine.

Adapted from Joshi, Denson & Downes, Quant Job Interview Questions and Answers, Q5.8, p.158; question and answer rewritten by SelfMadeQuant.com.

See the hint and the full worked solution in the question bank (free account).

Open the worked solution

Lessons behind this question