ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/common/tcos.c
Revision: 3.6
Committed: Thu Jan 17 22:48:21 2013 UTC (11 years, 3 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 3.5: +2 -2 lines
Log Message:
Increased table size from 256 to 1024

File Contents

# Content
1 #ifndef lint
2 static const char RCSid[] = "$Id: tcos.c,v 3.5 2011/02/11 00:35:14 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 rtmath.h
13 */
14
15 #include "copyright.h"
16
17 #include <math.h>
18
19 #include "rtmath.h"
20
21 #ifndef NCOSENTRY
22 #define NCOSENTRY 1024
23 #endif
24
25
26 double
27 tcos(double x) /* approximate cosine */
28 {
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 while (i >= 4*NCOSENTRY)
40 i -= 4*NCOSENTRY;
41 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 }