Code Comments

Programming Forum and web based access to our favorite programming groups.
For Programmers: Free Programming Magazines
Registration is free! Edit your profileCalendarFind other membersFrequently Asked QuestionsSearch -> 
Post New Thread











Thread
Author

preprocessor macros
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



Report this thread to moderator Post Follow-up to this message
Old Post
Jay Wolfe
02-18-05 01:58 AM


Re: preprocessor macros
"Jay Wolfe" <jayw710@adelphia.net> wrote in message
news:WYKdnT2YBMuDhojfRVn-3A@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 defi
ned text, which results in the cos(...) being calculated
> in my program every time. Is there a way to get the preprocessor to calcul
ate 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.[
/color]

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: donotspam_larry_brasfield@hotmail.com
Above views may belong only to me.



Report this thread to moderator Post Follow-up to this message
Old Post
Larry Brasfield
02-18-05 01:58 AM


Re: preprocessor macros
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 literall
y
what your interested in, you don't need to call cos at all.   The answer is 
.7071..

Report this thread to moderator Post Follow-up to this message
Old Post
Ron Natalie
02-18-05 01:58 AM


Re: preprocessor macros
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"

Report this thread to moderator Post Follow-up to this message
Old Post
E. Robert Tisdale
02-18-05 01:58 AM


Re: preprocessor macros
"Jay Wolfe" <jayw710@adelphia.net> wrote in message
news:WYKdnT2YBMuDhojfRVn-3A@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"



Report this thread to moderator Post Follow-up to this message
Old Post
Walter
02-18-05 01:58 AM


Sponsored Links




Last Thread Next Thread Next
Search this forum -> 
Post New Thread

C++ archive

Show a Printable Version Send to friend Email This Page to Someone! subscribe to this thread Receive updates to this thread
Computer Consultants
Programming Jobs
Visual Basic Controls
SQL Server Programming
Webservices
Java Security
Visual Studio
C# Programming
Visual J++
Software engineering
Open source Software
Perl Programming
PHP Programming
ASP Programming
ASP .NET Programming
Visual Basic Programming
Windows Scripting Host
Java Programming
Java Help
Java Beans
VBScript
Cobol
MAC Applications
Unix Programming
Forum Jump:
All times are GMT. The time now is 04:25 AM.

 

Programming forum archive

Copyrights CodeComments.com 2004 - 2006

Powered by vBulletin Copyright 2000-2006 Jelsoft Enterprises Limited.