1 |
#ifndef lint |
2 |
static const char RCSid[] = "$Id$"; |
3 |
#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 |
* |
12 |
* External symbols declared in standard.h |
13 |
*/ |
14 |
|
15 |
#include "copyright.h" |
16 |
|
17 |
#include <math.h> |
18 |
|
19 |
#ifndef NCOSENTRY |
20 |
#define NCOSENTRY 256 |
21 |
#endif |
22 |
|
23 |
#ifdef M_PI |
24 |
#define PI ((double)M_PI) |
25 |
#else |
26 |
#define PI 3.14159265358979323846 |
27 |
#endif |
28 |
|
29 |
|
30 |
double |
31 |
tcos(x) /* approximate cosine */ |
32 |
register double x; |
33 |
{ |
34 |
static double costab[NCOSENTRY+1]; |
35 |
register int i; |
36 |
|
37 |
if (costab[0] < 0.5) /* initialize table */ |
38 |
for (i = 0; i <= NCOSENTRY; i++) |
39 |
costab[i] = cos((PI/2./NCOSENTRY)*i); |
40 |
/* normalize angle */ |
41 |
if (x < 0.) |
42 |
x = -x; |
43 |
i = (NCOSENTRY*2./PI) * x + 0.5; |
44 |
if (i >= 4*NCOSENTRY) |
45 |
i %= 4*NCOSENTRY; |
46 |
switch (i / NCOSENTRY) { |
47 |
case 0: |
48 |
return(costab[i]); |
49 |
case 1: |
50 |
return(-costab[(2*NCOSENTRY)-i]); |
51 |
case 2: |
52 |
return(-costab[i-(2*NCOSENTRY)]); |
53 |
case 3: |
54 |
return(costab[(4*NCOSENTRY)-i]); |
55 |
} |
56 |
return(0.); /* should never be reached */ |
57 |
} |