Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > C Preprocessor

Reply
Thread Tools

C Preprocessor

 
 
sangeeta chowdhary
Guest
Posts: n/a
 
      06-30-2010
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.
 
Reply With Quote
 
 
 
 
Francois Grieu
Guest
Posts: n/a
 
      06-30-2010
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
 
Reply With Quote
 
 
 
 
Ike Naar
Guest
Posts: n/a
 
      06-30-2010
In article <(E-Mail Removed)>,
sangeeta chowdhary <(E-Mail Removed)> wrote:
>#define SQUARE(x) x*x
> [...]
> a=2*(s-u*t)/SQUARE(t);
> [...]
>Guys please help me.


Have a look at the C language FAQ, http://c-faq.com/cpp/index.html .
You asked question 10.1 .
 
Reply With Quote
 
Vincenzo Mercuri
Guest
Posts: n/a
 
      06-30-2010
sangeeta chowdhary wrote:
> 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.

--
Vincenzo Mercuri
 
Reply With Quote
 
sangeeta chowdhary
Guest
Posts: n/a
 
      06-30-2010
On Jun 30, 5:38*pm, pete <(E-Mail Removed)> 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),
> 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.
 
Reply With Quote
 
sangeeta chowdhary
Guest
Posts: n/a
 
      06-30-2010
On Jun 30, 3:33*pm, Francois Grieu <(E-Mail Removed)> wrote:
> 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


Thank You so much.
 
Reply With Quote
 
sangeeta chowdhary
Guest
Posts: n/a
 
      06-30-2010
On Jun 30, 3:43*pm, Richard Heathfield <(E-Mail Removed)> wrote:
> sangeeta chowdhary wrote:
> > Hi,
> > I am trying some code and this code astonished me with its output.

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


Thank You so much.
 
Reply With Quote
 
sangeeta chowdhary
Guest
Posts: n/a
 
      06-30-2010
On Jun 30, 4:28*pm, (E-Mail Removed) (Ike Naar) wrote:
> In article <(E-Mail Removed)..com>,
> sangeeta chowdhary *<(E-Mail Removed)> wrote:
>
> >#define SQUARE(x) x*x
> > [...]
> > *a=2*(s-u*t)/SQUARE(t);
> > [...]
> >Guys please help me.

>
> Have a look at the C language FAQ,http://c-faq.com/cpp/index.html.
> You asked question 10.1 .


Thank You so much.
 
Reply With Quote
 
Vincenzo Mercuri
Guest
Posts: n/a
 
      06-30-2010
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))...
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
 
Reply With Quote
 
Vincenzo Mercuri
Guest
Posts: n/a
 
      06-30-2010
pete wrote:
> Vincenzo Mercuri wrote:
>>
>> 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))...
>> 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) 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.
>


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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Compiler error occurred when try to use a flexible template expression in preprocessor definesCompiler error occurred when try to use a flexible template expression in preprocessor defines snnn C++ 6 03-14-2005 04:09 PM
C# Preprocessor =?Utf-8?B?SSBhbSBTYW0=?= ASP .Net 2 03-13-2005 02:47 PM
preprocessor, token concatenation, no valid preprocessor token Cronus C++ 1 07-14-2004 11:10 PM
VHDL Preprocessor The Weiss Family VHDL 2 07-14-2004 05:51 AM
Preprocessor conditional compilation variable not being saved Chris P ASP .Net 0 10-28-2003 08:48 PM



Advertisments