1 |
/* Copyright (c) 1998 Silicon Graphics, Inc. */ |
2 |
|
3 |
#ifndef lint |
4 |
static char SCCSid[] = "$SunId$ SGI"; |
5 |
#endif |
6 |
|
7 |
/* |
8 |
* Table-based cosine approximation. |
9 |
* |
10 |
* Use doubles in table even though we're not nearly that accurate just |
11 |
* to avoid conversion and guarantee that tsin(x)^2 + tcos(x)^2 == 1. |
12 |
* |
13 |
* No interpolation in this version. |
14 |
*/ |
15 |
|
16 |
#include <math.h> |
17 |
|
18 |
#ifndef NCOSENTRY |
19 |
#define NCOSENTRY 256 |
20 |
#endif |
21 |
|
22 |
#ifdef M_PI |
23 |
#define PI ((double)M_PI) |
24 |
#else |
25 |
#define PI 3.14159265358979323846 |
26 |
#endif |
27 |
|
28 |
|
29 |
double |
30 |
tcos(x) /* approximate cosine */ |
31 |
register double x; |
32 |
{ |
33 |
static double costab[NCOSENTRY+1]; |
34 |
register int i; |
35 |
|
36 |
if (costab[0] < 0.5) /* initialize table */ |
37 |
for (i = 0; i <= NCOSENTRY; i++) |
38 |
costab[i] = cos((PI/2./NCOSENTRY)*i); |
39 |
/* normalize angle */ |
40 |
if (x < 0.) |
41 |
x = -x; |
42 |
i = (NCOSENTRY*2./PI) * x + 0.5; |
43 |
if (i >= 4*NCOSENTRY) |
44 |
i %= 4*NCOSENTRY; |
45 |
switch (i / NCOSENTRY) { |
46 |
case 0: |
47 |
return(costab[i]); |
48 |
case 1: |
49 |
return(-costab[(2*NCOSENTRY)-i]); |
50 |
case 2: |
51 |
return(-costab[i-(2*NCOSENTRY)]); |
52 |
case 3: |
53 |
return(costab[(4*NCOSENTRY)-i]); |
54 |
} |
55 |
return(0.); /* should never be reached */ |
56 |
} |