Back to index

radiance  4R0+20100331
tcos.c
Go to the documentation of this file.
00001 #ifndef lint
00002 static const char    RCSid[] = "$Id: tcos.c,v 3.4 2003/07/17 09:21:29 schorsch Exp $";
00003 #endif
00004 /*
00005  * Table-based cosine approximation.
00006  *
00007  * Use doubles in table even though we're not nearly that accurate just
00008  * to avoid conversion and guarantee that tsin(x)^2 + tcos(x)^2 == 1.
00009  *
00010  * No interpolation in this version.
00011  *
00012  * External symbols declared in standard.h
00013  */
00014 
00015 #include "copyright.h"
00016 
00017 #include <math.h>
00018 
00019 #include "rtmath.h"
00020 
00021 #ifndef NCOSENTRY
00022 #define NCOSENTRY    256
00023 #endif
00024 
00025 #ifdef M_PI
00026 #define PI           ((double)M_PI)
00027 #else
00028 #define PI           3.14159265358979323846
00029 #endif
00030 
00031 
00032 double
00033 tcos(x)                            /* approximate cosine */
00034 register double      x;
00035 {
00036        static double costab[NCOSENTRY+1];
00037        register int  i;
00038 
00039        if (costab[0] < 0.5)        /* initialize table */
00040               for (i = 0; i <= NCOSENTRY; i++)
00041                      costab[i] = cos((PI/2./NCOSENTRY)*i);
00042                                    /* normalize angle */
00043        if (x < 0.)
00044               x = -x;
00045        i = (NCOSENTRY*2./PI) * x  +  0.5;
00046        if (i >= 4*NCOSENTRY)
00047               i %= 4*NCOSENTRY;
00048        switch (i / NCOSENTRY) {
00049        case 0:
00050               return(costab[i]);
00051        case 1:
00052               return(-costab[(2*NCOSENTRY)-i]);
00053        case 2:
00054               return(-costab[i-(2*NCOSENTRY)]);
00055        case 3:
00056               return(costab[(4*NCOSENTRY)-i]);
00057        }
00058        return(0.);          /* should never be reached */
00059 }