QUOTE(prads @ 25 Jun, 2008 - 05:49 PM)

Do I have to run the code separately and then store all the values in the arrays sin_table[quarter_table_size] and cos_table[quarter_table_size], and then pull the values for its respective arguments? i.e do I have to manually generate sin and cos values in radians and store it first before running your code?
No, you don't. Looking at the following section:
cpp
if(!sin_table[k].is_valid)
{ sin_table[k].result = sin((double)k / 10000.0);
sin_table[k].is_valid = true;
}
Since the sin_table is zero'ed in init_tables, the is_valid member of each structure is false by default, and is only true once the inside of that if block is executed (which also uses the library sin function to calculate the value). So it fills the entries on demand.
You don't need a separate cosine table, as cosine is the same as sine shifted 90 degrees (as shown by my definition of dyn_cos in my previous post).
QUOTE
What changes to be made if I want to generate it statically (because the value of v is different each time)?
One way that you could do this would be by taking the core of dyn_sin so that it computes all values at once, such as:
cpp
void compute_sin_table()
{
for(k = 0; k < quarter_table_size; k++)
sin_table[k].result = sin((double)k / 10000.0);
}
double static_sin(double v)
{
int k = (int)(v * 10000) % (quarter_table_size * 4);
double mul = 1.0;
if(k >= quarter_table_size && k < quarter_table_size * 2) k = (quarter_table_size * 2) - k;
else if(k >= quarter_table_size * 2 && k < quarter_table_size * 3) { k = k - (quarter_table_size * 2); mul = -1.0; }
else if(k >= quarter_table_size * 3) { k = (quarter_table_size * 4) - k; mul = -1.0; }
return sin_table[k].result * mul;
}
double static_cos(double v)
{
return static_sin(v + (PI / 2.0));
}
Of course, each element of sin_table could now be a double instead of a struct, because is_valid is no longer needed.
For optimal effect, you could write a program that computes this table, and prints it out in the form of a C++ snippet. It would need to print something like:
CODE
double sin_table[quarter_table_size] = {
0.00000, 0.00001, ......
};
So that you can copy and paste that hard-coded array into your program, so the table never needs to be computed to begin with.
QUOTE
I ran the below code but it has no value stored in the array sin_table[quarter_table_size]. Please make it more clear for me.
The code will compute one value at a time, and will cache the value, so the next time the function is called with the same value, it just returns the stored value in sin_table.
QUOTE
Also i didnot understand the working of void init_tables() . Is this storing the sin and cos values?
No. This initializes the sin_table array to all zeros, so that all is_valid items are false initially. The dyn_sin function caches the values on demand.
If you call dyn_sin(0.3) for the first time, the library sin function is called. If you call it again (with the same value, 0.3), it takes it from the lookup table, as the function records the result from sin whenever it calls it.