By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,413 Members | 996 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,413 IT Pros & Developers. It's quick & easy.

preprocessor macros

P: n/a
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
Jul 23 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
"Jay Wolfe" <ja*****@adelphia.net> wrote in message
news:WY********************@adelphia.com...
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++.

--
--Larry Brasfield
email: do***********************@hotmail.com
Above views may belong only to me.
Jul 23 '05 #2

P: n/a
Jay Wolfe wrote:
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..
Jul 23 '05 #3

P: n/a
Jay Wolfe wrote:


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"
Jul 23 '05 #4

P: n/a

"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"
Jul 23 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.