1 |
gwlarson |
3.1 |
#ifndef lint |
2 |
greg |
3.5 |
static const char RCSid[] = "$Id: tcos.c,v 3.4 2003/07/17 09:21:29 schorsch Exp $"; |
3 |
gwlarson |
3.1 |
#endif |
4 |
|
|
/* |
5 |
|
|
* Table-based cosine approximation. |
6 |
|
|
* |
7 |
|
|
* Use doubles in table even though we're not nearly that accurate just |
8 |
|
|
* to avoid conversion and guarantee that tsin(x)^2 + tcos(x)^2 == 1. |
9 |
|
|
* |
10 |
|
|
* No interpolation in this version. |
11 |
greg |
3.2 |
* |
12 |
greg |
3.5 |
* External symbols declared in rtmath.h |
13 |
greg |
3.2 |
*/ |
14 |
|
|
|
15 |
greg |
3.3 |
#include "copyright.h" |
16 |
gwlarson |
3.1 |
|
17 |
|
|
#include <math.h> |
18 |
schorsch |
3.4 |
|
19 |
|
|
#include "rtmath.h" |
20 |
gwlarson |
3.1 |
|
21 |
|
|
#ifndef NCOSENTRY |
22 |
|
|
#define NCOSENTRY 256 |
23 |
|
|
#endif |
24 |
|
|
|
25 |
|
|
|
26 |
|
|
double |
27 |
greg |
3.5 |
tcos(double x) /* approximate cosine */ |
28 |
gwlarson |
3.1 |
{ |
29 |
|
|
static double costab[NCOSENTRY+1]; |
30 |
|
|
register int i; |
31 |
|
|
|
32 |
|
|
if (costab[0] < 0.5) /* initialize table */ |
33 |
|
|
for (i = 0; i <= NCOSENTRY; i++) |
34 |
|
|
costab[i] = cos((PI/2./NCOSENTRY)*i); |
35 |
|
|
/* normalize angle */ |
36 |
|
|
if (x < 0.) |
37 |
|
|
x = -x; |
38 |
|
|
i = (NCOSENTRY*2./PI) * x + 0.5; |
39 |
greg |
3.5 |
while (i >= 4*NCOSENTRY) |
40 |
|
|
i -= 4*NCOSENTRY; |
41 |
gwlarson |
3.1 |
switch (i / NCOSENTRY) { |
42 |
|
|
case 0: |
43 |
|
|
return(costab[i]); |
44 |
|
|
case 1: |
45 |
|
|
return(-costab[(2*NCOSENTRY)-i]); |
46 |
|
|
case 2: |
47 |
|
|
return(-costab[i-(2*NCOSENTRY)]); |
48 |
|
|
case 3: |
49 |
|
|
return(costab[(4*NCOSENTRY)-i]); |
50 |
|
|
} |
51 |
|
|
return(0.); /* should never be reached */ |
52 |
|
|
} |