"Jay Wolfe" <ja*****@adelphia.net> wrote in message

news:WY********************@adelphia.com...

Hi,

If I define a macro as follows

#define COSTHETA cos(45.0*3.14159256/180.0)

I believe that any time COSTHETA appears it is simply replaced by the

defined text, which results in the cos(...) being calculated in my program

every time. Is there a way to get the preprocessor to calculate the cosine

and just replace COSTHETA with its final number? I know I could define a

const variable but I just want the final number.

Thanks for any help.

#include <math.h>

#define COSTHETA (M_SQRT2 / 2)

should also work (even though M_SQRT2 is not part of the Standard, it is

commonly defined). BTW, the value of pi you're using is inaccurate and will

give poor results. It also has the last two digits transposed. Use as many

digits as the precision allows, this one will work a bit better:

3.14159265358979323846, or even better, use M_PI if your math.h has it.

Best is to use a precalculated PI that is accurate to within one bit:

0x1.921fb54442d1846ap+1L has 64 bits of mantissa.

If you want to use angles other than 45 degrees, an easy solution is to

write a simple C program that calculates the values, and then prints the

results out in a .c program which is then compiled into the application. I

use this all the time to generate complicated, but fixed, tables that are

way beyond what the preprocessor can do.

-Walter

www.digitalmars.com free C, C++, D compilers

"code of the nerds"