C Preprocessing With Tcl

By Jonathan S. Arney

Dr. Dobb's Journal August 1998

(a)
#include <stdio.h>
#$
set pi 3.14159265359
puts stdout "float s_cos_table\[181\] = {"
set deg 0
while { 1 } {
	puts stdout "[expr cos(($deg*$pi)/180) ]" nonewline 
		incr deg 
	if { $deg == 180 } break; 
	puts stdout "," 
}
puts stdout "};"
proc c_cos {a} { \
	puts stdout s_cos_table\\[$a\\] \
}
#$
int main(int argc, char **argv)
{
	int     i;
	for (i = 0; i < 180; i++) {
		printf("Cosine of %i is %f\n", i, #$c_cos i#$);
	}
}
(b)
#include <stdio.h>
float s_cos_table[181] = {
1.0,
0.999848,
 ...
-0.999391,
-0.999848};
int main(int argc, char **argv)
{
	int     i;
	for (i = 0; i < 180; i++) {
		printf("Cosine of %i is %f\n", i, s_cos_table[i]);
	}
}

Example 3: (a) Source code for generating a cosine lookup table; (b) generated cosine table.

Back to Article


Copyright © 1998, Dr. Dobb's Journal