Skip to the content of the web site.

Lesson 12: Our approximation of sin

You may be wondering where this function came from: it is a cubic polynomial and if you were to evaluate it at $0$ and $\frac{\pi}{2}$, you would get the values $0$ and $1$, respectively, and if you were to evaluate the slopes (the derivative) at $0$ and $\frac{\pi}{2}$, you would get $1$ and $0$, respectively. To get this, we required mathematics:

If we want the polynomial

$$ax^3 + bx^2 + cx + d$$

to satisfy

$$a\cdot 0^3 + b\cdot 0^2 + c \cdot 0 + d = 0$$

it follows that $d = 0$. The derivative of $ax^3 + bx^2 + cx$ is $3ax^2 + 2bx + c$, and if the slope at $x = 0$ is $1$, then

$$3a\cdot 0^2 + 2b\cdot 0 + c = 1$$

so it follows that $c = 1$. Thus, our polynomial must be

$$a\cdot x^3 + b\cdot x^2 + x$$

We now require that the value of this polynomial at $x = \frac{\pi}{2}$ is $1$ and the derivative at this point is $0$. This gives us the two equations $$a\left(\frac{\pi}{2}\right)^3 + b\left(\frac{\pi}{2}\right)^2 + \frac{\pi}{2} = 1$$ $$3a\left(\frac{\pi}{2}\right)^2 + 2b\frac{\pi}{2} + 1 = 0$$

These give us two linear equations in two unknowns, and while $$\frac{\pi^3}{8}a + \frac{\pi^2}{4}b = 1 - \frac{\pi}{2}$$ $$\frac{3\pi^2}{4}a + \pi b = -1$$

Adding $-\frac{6}{\pi}$ times the first equation onto the second eliminates $a$ from the second equation, so we have that

$$-\frac{\pi}{2}b = -1 - \frac{6}{\pi}\left(1 - \frac{\pi}{2}\right)$$

or, solving for $b$, we have

$$b = \frac{4(\pi - 3)}{\pi^2}$$

Substituting this into the first equation yields

$$a = \frac{4(\pi - 4)}{\pi^3}$$

If you approximate $a$ and $b$ to 17 digits of precision, you get the approximations you see in the fast_sin:

double fast_sin( double x ) {
	return -0.11073981636184074*x*x*x - 0.057385341027109429*x*x + x;
}