cos() and sin()
1. Taylor Series
通过Taylor series(泰勒级数)获得$\sin(x)$和$\cos(x)$的多项式表达
$$
\because f(x) = f(a) + \frac{f'(a)}{1!}(x-a) + \frac{f^{(2)}(a)}{2!}(x-a)^2 + \cdots + \frac{f^{(n)}(a)}{n!}(x-a)^n \\
\therefore sin(x) = \frac{cos(0)}{1!} - \frac{cos(0)}{3!}x^3 + \frac{cos(0)}{5!}x^5 - \frac{cos(0)}{7!}x^7 + \frac{cos(0)}{9!}x^9 \cdots \\
= 1 - \frac{1}{3!}x^3 + \frac{1}{5!}x^5 - \frac{1}{7!}x^7 + \frac{1}{9!}x^9 \cdots \\
cos(x) = cos(0) + \frac{cos(0)}{2!}x^2 - \frac{cos(0)}{4!}x^4 + \frac{cos(0)}{6!}x^6 - \frac{cos(0)}{8!}x^8 \cdots \\
= 1 + \frac{1}{2!}x^2 - \frac{1}{4!}x^4 + \frac{1}{6!}x^6 - \frac{1}{8!}x^8 \cdots
$$
2. Narrow down the Range
在使用泰勒级数前, 需要先缩减x
的区间:
- 利用周期性将x的范围缩减到$(-\pi, \pi]$
- 利用奇偶性将x的范围缩减到$[0, \pi]$
- 利用诱导公式将x的范围缩减到$[-\frac{\pi}{2}, \frac{\pi}{2}]$
- 利用诱导公式将x的范围缩减到$[-\frac{\pi}{4}, \frac{\pi}{4}]$
诱导公式:
- $\sin(\pi + \alpha) = -\sin \alpha$
- $\cos(\pi + \alpha) = -\cos \alpha$
- $\sin(\frac{\pi}{2} - \alpha) = \cos \alpha$
- $\cos(\frac{\pi}{2} - \alpha) = \sin \alpha$
3. Implementation
private static final double PI = 3.14159265358979323846; |