C Preprocessor

S

sangeeta chowdhary

Hi,
I am trying some code and this code astonished me with its output.

#include<stdio.h>
#define SQUARE(x) x*x

int main()
{
float s=10,u=30,t=2,a;
a=2*(s-u*t)/SQUARE(t);
printf("Result: %f\n",a);
return 0;
}

output: -100.000000

If I if I store result of a macro in avariable like

x=SQUARE(t);
a=2*(s-u*t)/x;

then output is coming -25 (expected) .

Guys please help me.
 
F

Francois Grieu

Le 30/06/2010 12:07, sangeeta chowdhary a écrit :
Hi,
I am trying some code and this code astonished me with its output.

#include<stdio.h>
#define SQUARE(x) x*x

int main()
{
float s=10,u=30,t=2,a;
a=2*(s-u*t)/SQUARE(t);
printf("Result: %f\n",a);
return 0;
}

output: -100.000000

If I if I store result of a macro in avariable like

x=SQUARE(t);
a=2*(s-u*t)/x;

then output is coming -25 (expected) .

Guys please help me.

Hint: as your compiler to show the preprocessor output.

Giveaway: you want
#define SQUARE(x) ((x)*(x))


Francois Grieu
 
V

Vincenzo Mercuri

sangeeta said:
Hi,
I am trying some code and this code astonished me with its output.

#include<stdio.h>
#define SQUARE(x) x*x

int main()
{
float s=10,u=30,t=2,a;
a=2*(s-u*t)/SQUARE(t);
printf("Result: %f\n",a);
return 0;
}

output: -100.000000

If I if I store result of a macro in avariable like

x=SQUARE(t);
a=2*(s-u*t)/x;

1) If /t/ is float (or double), I'd rather call pow(t,2)
[after #include <math.h>]

2) If /t/ is an /int/ I'd rather use SQUARE(t) [#define SQUARE(x) ((x)*(x))]
to avoid rounding errors in pow(int, int) [this prototype doesnt exist].
I should *never use SQUARE(t++)* though, to avoid side effects.
 
S

sangeeta chowdhary

Vincenzo said:
sangeeta said:
#define SQUARE(x) x*x
1) If /t/ is float (or double), I'd rather call pow(t,2)
    [after #include <math.h>]

I would never write pow(t,2) instead of (t*t),
when (t) is the declared identifier
of an object with arithmetic type.

If anybody has any trouble understanding (t*t),
then they can't understand pow(t,2).

pow(t,2) is a big solution to a small problem.

Thank You so much.
 
S

sangeeta chowdhary

Le 30/06/2010 12:07, sangeeta chowdhary a crit :











Hint: as your compiler to show the preprocessor output.

Giveaway: you want
#define SQUARE(x) ((x)*(x))

  Francois Grieu

Thank You so much.
 
S

sangeeta chowdhary

Change this to:

#define SQUARE(x) (x*x)

Re-test.

Think about the change in your results.

It may help to consult page 53 of K&R2.

<snip>

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
"Usenet is a strange place" - dmr 29 July 1999
Sig line vacant - apply within

Thank You so much.
 
V

Vincenzo Mercuri

pete said:
Vincenzo said:
sangeeta chowdhary wrote:
1) If /t/ is float (or double), I'd rather call pow(t,2)
[after #include<math.h>]

I would never write pow(t,2) instead of (t*t),

....Richard would be happy to know that you
appreciate his style, (t*t) instead of ((t)*(t))...
joke aside...

you could never do this:

t = 1;
while(...)
SQUARE(t++);

but you can do:

t = 1;
while(...)
square(t++);

with square being a function like:

<type> square(<type> t){
return t*t;
}

one for each arithmetic <type>.

This is ok as long as your calculations dont need
to raise a power to an exponent greater than 2 or however
small exponents. Frankly it doesnt make any substantial
difference.
pow has been mainly implemented to easily handle
cases in which the exponent is a variable on a wide range
and when it is not of an integer type.
That said, good #macros and no side-effects are mostly welcome
pow(t,2) is a big solution to a small problem.

pow(t,2) yes, but pow is a great solution in the general case
 
V

Vincenzo Mercuri

pete said:
Vincenzo said:
Vincenzo Mercuri wrote:

sangeeta chowdhary wrote:

#define SQUARE(x) x*x

1) If /t/ is float (or double), I'd rather call pow(t,2)
[after #include<math.h>]

I would never write pow(t,2) instead of (t*t),

...Richard would be happy to know that you
appreciate his style, (t*t) instead of ((t)*(t))...
joke aside...

I am not talking about the macro anymore.
Rather than write either

SQUARE(x)

or

pow(t,2)

I would write

(t * t)

instead.
pow(t,2) yes, but pow is a great solution in the general case

(t * t) works for any arithmetic type.
You are only recommending pow(t,2) for floating point types.

(t * t) is portable on freestanding implementations.
pow(t,2) isn't portable on freestanding implementations.

comparing (t*t) to pow(t,2) doesn't make sense.
It depends on what you have to achieve.
If you intend to do

t=2; and then (t*t)... i prefer 2*2

would you like to do (t++*t++) ?

even on a freestanding implementation it wouldnt work as you desire.
 
V

Vincenzo Mercuri

Richard said:
Vincenzo said:
pete said:
Vincenzo Mercuri wrote:

sangeeta chowdhary wrote:

#define SQUARE(x) x*x

1) If /t/ is float (or double), I'd rather call pow(t,2)
[after #include<math.h>]

I would never write pow(t,2) instead of (t*t),

....Richard would be happy to know that you
appreciate his style, (t*t) instead of ((t)*(t))...

No, he would be far happier if you were to appreciate the importance of
the extra ()s that I so carelessly omitted upthread.

<snip>

Well... personally I appreciate new ideas way more than a fortuitous
lack of parenthesis
 
V

Vincenzo Mercuri

Richard said:
Vincenzo Mercuri wrote:



It sure makes sense to me.


I prefer 4


Clearly not. Write what you mean. If the intent is to assign the square
of t's value, incrementing t along the way, then it makes more sense to
do this:

lv = t * t;
++t;

Of course. I agree. I was ironically highlighting the
difference between pow(t, 2) and (t*t). Clearly, as I said,
there is not problem when the exponent is 2. They are almost
interchangeable. But i don't think it would be so easy to use
multiple products if I had to compute 2^13, 2^14, 2^6,
with a variable exponent. At least, writing t*t*...*t
doesnt seem to be the right path.
 
V

Vincenzo Mercuri

Vincenzo said:
Richard said:
Vincenzo said:
pete wrote:
Vincenzo Mercuri wrote:

sangeeta chowdhary wrote:

#define SQUARE(x) x*x

1) If /t/ is float (or double), I'd rather call pow(t,2)
[after #include<math.h>]

I would never write pow(t,2) instead of (t*t),

....Richard would be happy to know that you
appreciate his style, (t*t) instead of ((t)*(t))...

No, he would be far happier if you were to appreciate the importance of
the extra ()s that I so carelessly omitted upthread.

<snip>

Well... personally I appreciate new ideas way more than a fortuitous
lack of parenthesis
In the sense that i dont care of missing parenthesis.
Humans are supposed to be imperfect.
(sorry for my english!)
 
V

Vincenzo Mercuri

pete said:
Vincenzo said:
Vincenzo Mercuri wrote:

sangeeta chowdhary wrote:

#define SQUARE(x) x*x

1) If /t/ is float (or double), I'd rather call pow(t,2)
[after #include<math.h>]

I would never write pow(t,2) instead of (t*t),

...Richard would be happy to know that you
appreciate his style, (t*t) instead of ((t)*(t))...
joke aside...

I am not talking about the macro anymore.
Rather than write either

SQUARE(x)

or

pow(t,2)

I would write

(t * t)

instead.
pow(t,2) yes, but pow is a great solution in the general case

(t * t) works for any arithmetic type.
You are only recommending pow(t,2) for floating point types.

(t * t) is portable on freestanding implementations.
pow(t,2) isn't portable on freestanding implementations.

I must admit that here I awfully explained what my point
was. In my explanation I had in mind the case of
pow(t,n) with n>2 . So I think I went a little OT.
And I can't mention, maybe there isn't, any example in which
pow(t,2) would be better than (t*t). I got later that you were
talking about a simple expression instead of a macro, and that
you just wanted to focus on the square of an integer.

I don't know why I have been so obtuse to carry on my
ideas about t^n instead of carefully reading your answers.

Cheers
 

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

Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,527
Members
45,000
Latest member
MurrayKeync

Latest Threads

Top