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