Turns out, it only needed to be degree 10!
This commit is contained in:
parent
664c0d433a
commit
a51c11701f
1 changed files with 2 additions and 13 deletions
15
math/cos.c
15
math/cos.c
|
@ -10,14 +10,13 @@
|
|||
|
||||
The taylor poly is centered at pi, with a radius of
|
||||
convergence of no less than pi, making it roughly accurate
|
||||
between 0 and 2 * pi. The polynomial is up to x^20, so it
|
||||
should be pretty accurate within that range.
|
||||
between 0 and 2 * pi. The polynomial is up to x^10, which I've
|
||||
confirmed as being accurate being 0 and 2 * pi.
|
||||
*/
|
||||
double cos(double x) {
|
||||
double pi = M_PI; /* Really, me?! -Kat */
|
||||
int temp;
|
||||
double deg_2, deg_4, deg_6, deg_8, deg_10;
|
||||
double deg_12, deg_14, deg_16, deg_18, deg_20;
|
||||
double cosine;
|
||||
|
||||
if(x < 0) x = -x;
|
||||
|
@ -44,11 +43,6 @@ double cos(double x) {
|
|||
deg_6 = deg_4 * deg_2 * 2 / (6 * 5);
|
||||
deg_8 = deg_6 * deg_2 * 2 / (8 * 7);
|
||||
deg_10 = deg_8 * deg_2 * 2 / (10 * 9);
|
||||
deg_12 = deg_10 * deg_2 * 2 / (12 * 11);
|
||||
deg_14 = deg_12 * deg_2 * 2 / (14 * 13);
|
||||
deg_16 = deg_14 * deg_2 * 2 / (16 * 15);
|
||||
deg_18 = deg_16 * deg_2 * 2 / (18 * 17);
|
||||
deg_20 = deg_18 * deg_2 * 2 / (20 * 19);
|
||||
|
||||
/*
|
||||
In case you aren't familiar with the theory of a Taylor
|
||||
|
@ -79,13 +73,8 @@ double cos(double x) {
|
|||
Plus, since we're taking advantage of cosine being an
|
||||
even function, we don't need any terms less than 0, so
|
||||
why *center* at zero and include negatives?
|
||||
(Of course, I've not done the radius of convergence work
|
||||
for this to find out how many degrees we need to get a
|
||||
radius of convergence of pi, so I may have used too many
|
||||
terms, anyways. Not that I can't reduce that...)
|
||||
*/
|
||||
cosine = -1 + deg_2 - deg_4 + deg_6 - deg_8 + deg_10;
|
||||
cosine = cosine - deg_12 + deg_14 - deg_16 + deg_18 + deg_20;
|
||||
|
||||
return cosine;
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue