Hint: as your compiler to show the preprocessor output.
Giveaway: you want
#define SQUARE(x) ((x)*(x))
Francois Grieu
Why?
>
> #include<stdio.h>
> #define SQUARE(x) x*x
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
Instead of doing it right?
#define SQUARE(x) ((x) * (x))
I consider some things,
such as when you can get away with
deviating from good macro writing style,
to be not worth thinking about.
--
pete
Touche'.
Have a look at the C language FAQ, http://c-faq.com/cpp/index.html .
You asked question 10.1 .
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.
--
Vincenzo Mercuri
> > #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.
--
pete
Thank You so much.
Thank You so much.
Thank You so much.
Thank You so much.
...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
--
Vincenzo Mercuri
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) is a big solution to a small problem.
>
> 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.
--
pete
No, he would be far happier if you were to appreciate the importance of
the extra ()s that I so carelessly omitted upthread.
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.
--
Vincenzo Mercuri
<snip>
> comparing (t*t) to pow(t,2) doesn't make sense.
It sure makes sense to me.
> It depends on what you have to achieve.
> If you intend to do
>
> t=2; and then (t*t)... i prefer 2*2
I prefer 4
> would you like to do (t++*t++) ?
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;
Well... personally I appreciate new ideas way more than a fortuitous
lack of parenthesis
--
Vincenzo Mercuri
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.
--
Vincenzo Mercuri
--
Vincenzo Mercuri
"Be the preprocessor" is the way to enlightenment, my son.
Expand "a=2*(s-u*t)/SQUARE(t);", and all shall be revealed.
--
Kenneth Brody
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
--
Vincenzo Mercuri