P: n/a

Hi group,
This is a followup of the question I posted this afternoon.
I tried to define two constants, PI and radian in that program in the
following way,
#define PI 3.1415926
#define radian 180./PI
I wonder whether they are the same as
static double PI=3.1415926;
static double radian=180./PI;
If they are not, why?
Thanks!
Regards,
Fan  
Share this Question
P: n/a

On Tue, 12 Oct 2004, Fan Zhang wrote: I tried to define two constants, PI and radian in that program in the following way, #define PI 3.1415926 #define radian 180./PI
I wonder whether they are the same as static double PI=3.1415926; static double radian=180./PI;
They are not. For a simple example of one major difference, try
compiling and running
int main(void)
{
printf("%g\n", 1/radian);
return 0;
}
with the two different definitions. If you're using a C compiler,
one of them won't even compile at all! (Read the FAQ.) If you
try it with a C++ compiler (comp.lang.c++ is down the hall), you'll
find that you get two different answers. (Read the FAQ.)
HTH,
Arthur  
P: n/a

"Fan Zhang" <fa******@sas.upenn.edu> writes: I tried to define two constants, PI and radian in that program in the following way, #define PI 3.1415926 #define radian 180./PI
I wonder whether they are the same as static double PI=3.1415926; static double radian=180./PI;
No. The former definition of radian is dangerous, because
writing x/radian will not have the intended effect. The latter
definitions cannot be used in constant expressions, and their
values can change. (Even if you make them `const', they still
cannot be used in constant expressions.)

"I don't have C&V for that handy, but I've got Dan Pop."
E. Gibbons  
P: n/a

Fan Zhang wrote: Hi group,
This is a followup of the question I posted this afternoon.
I tried to define two constants, PI and radian in that program in the following way, #define PI 3.1415926 #define radian 180./PI
I wonder whether they are the same as static double PI=3.1415926; static double radian=180./PI;
If they are not, why?
They are not. The latter two create actual objects in memory,
which in turn must be initialized with compile time constants,
which the initialization of radian is not (and should give a
compile time error).

Chuck F (cb********@yahoo.com) (cb********@worldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!  
P: n/a

Thanks for everybody's input.
This is what I found out last night after playing with it at home.
It is not a good way to write
#define radian 180./PI, although it is legal in my compiler (DevC++). The
problem with it is #define does not do the calculation, it just does the
REPLACEMENT! That is to say, when I tried to calculate 1./radian, it did
1./180./PI instead of 1./(180./PI). That is why the result did not make
sense in my program, at least it appears to be. :)
Thanks again!
"Fan Zhang" <fa******@sas.upenn.edu> wrote in message
news:ck***********@netnews.upenn.edu... Hi group,
This is a followup of the question I posted this afternoon.
I tried to define two constants, PI and radian in that program in the following way, #define PI 3.1415926 #define radian 180./PI
I wonder whether they are the same as static double PI=3.1415926; static double radian=180./PI;
If they are not, why?
Thanks!
Regards, Fan
 
P: n/a

"Fan Zhang" <fa******@sas.upenn.edu> writes: It is not a good way to write #define radian 180./PI, although it is legal in my compiler (DevC++). The problem with it is #define does not do the calculation, it just does the REPLACEMENT! That is to say, when I tried to calculate 1./radian, it did 1./180./PI instead of 1./(180./PI). That is why the result did not make sense in my program, at least it appears to be. :)
You can fix the problem with parentheses:
#define radian (180./PI)

"Given that computing power increases exponentially with time,
algorithms with exponential or better Onotations
are actually linear with a large constant."
Mike Lee  
P: n/a

"Fan Zhang" <fa******@sas.upenn.edu> wrote in message news:<ck***********@netnews.upenn.edu>... Hi group,
This is a followup of the question I posted this afternoon.
I tried to define two constants, PI and radian in that program in the following way, #define PI 3.1415926 #define radian 180./PI
I wonder whether they are the same as static double PI=3.1415926; static double radian=180./PI;
If they are not, why?
Thanks!
Regards, Fan
They are not equivalent. Preprocessor macros do not occupy memory the
way variables do, and they do not obey the same scope and visibility
rules as variables. Preprocessor macros are simply text strings that
are replaced in the source before it is actually compiled.
And your second example won't compile as written. Initializers for
static or extern variables must be compiletime constants, which
180./PI isn't.  
P: n/a

Fan Zhang wrote: It is not a good way to write #define radian 180./PI, although it is legal in my compiler (DevC++). The problem with it is #define does not do the calculation, it just does the REPLACEMENT! That is to say, when I tried to calculate 1./radian, it did 1./180./PI instead of 1./(180./PI). That is why the result did not make sense in my program, at least it appears to be. :)
That is why #defines are normally guarded by parentheses, as in:
#define radian (180/PI)
Please don't toppost. Your answer belongs after, or intermixed
with, the material to which you reply after snipping anything that
is not germane to your reply. Topposting is considered rude and
boorish in the better newsgroups.

Chuck F (cb********@yahoo.com) (cb********@worldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!  
P: n/a

CBFalconer wrote: Fan Zhang wrote: It is not a good way to write #define radian 180./PI, although it is legal in my compiler (DevC++). The problem with it is #define does not do the calculation, it just does the REPLACEMENT! That is to say, when I tried to calculate 1./radian, it did 1./180./PI instead of 1./(180./PI). That is why the result did not make sense in my program, at least it appears to be. :)
That is why #defines are normally guarded by parentheses, as in:
#define radian (180/PI)
For me, it's always been simpler to consider the word "radian"
as meaningless in mathematic expressions.
From there, it follows that the symbol for degrees
is a numeric constant.
/* BEGIN new.c */
#include <stdio.h>
#include <math.h>
#define pi 3.14159265358979
#define degrees (pi / 180)
int main(void)
{
printf("The sine of 90 degrees is %f\n", sin(90 * degrees));
return 0;
}
/* END new.c */

pete   This discussion thread is closed Replies have been disabled for this discussion.   Question stats  viewed: 6304
 replies: 8
 date asked: Nov 14 '05
