preprocessor macros

J

Jay Wolfe

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.

Jay
 
L

Larry Brasfield

Jay Wolfe said:
Hi, 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.

In C++, it would better to write something like:
static const double CosineEightTurn = cos(45.0*3.14159256/180.0);
Using macros for constants is bad form in C++.
 
R

Ron Natalie

Jay said:
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.
The preprocessor knows nothing about cos and can't really do anything
with floating point. There's not even a guarantee that the expression that
is the arg to your cos call is going to be evaluated at compile time
(the language makes no constraints on being able to do floating point
math, just integral math).

The const variable is the way to go, hoping that the compiler is smart
enough to do something optimal with it. Of course if the above is literally
what your interested in, you don't need to call cos at all. The answer is .7071..
 
E

E. Robert Tisdale

Jay said:
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.

Not necessarily.
Is there a way to get the preprocessor to calculate the cosine
and just replace COSTHETA with its final number?

No. But a good optimizing compiler should be able to do it for you.
I know I could define a const variable but I just want the final number.

Notice that cos(45.0*3.14159256/180.0) == sqrt(0.5).
If your compiler won't calculate cos(45.0*3.14159256/180.0),
it may still calculate sqrt(0.5):
cat costheta.cc
#include <math.h>

double costheta(void) {
return sqrt(0.5);
}
g++ -Wall -ansi -pedantic -O3 -S costheta.cc
cat costheta.s
.file "costheta.cc"
.section .rodata.cst8,"aM",@progbits,8
.align 8
.LC0:
.long 1719614413
.long 1072079006
.text
.align 2
.p2align 4,,15
.globl _Z8costhetav
.type _Z8costhetav, @function
_Z8costhetav:
.LFB5:
pushl %ebp
.LCFI0:
movl %esp, %ebp
.LCFI1:
popl %ebp
fldl .LC0
ret
.LFE5:
.size _Z8costhetav, .-_Z8costhetav
.section .note.GNU-stack,"",@progbits
.ident "GCC: (GNU) 3.4.1"
 
W

Walter

Jay Wolfe said:
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"
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Order of preprocessor macro replacement 3
Endianness macros 48
preprocessor bug? 2
How to fix this code? 1
macros question about new operator replacement 4
Preprocessor 4
Preprocessor trick 6
Macros 3

Members online

Forum statistics

Threads
473,768
Messages
2,569,575
Members
45,053
Latest member
billing-software

Latest Threads

Top