Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > GURU NEEDED : macro SQUARE(x) for any type x

Reply
Thread Tools

GURU NEEDED : macro SQUARE(x) for any type x

 
 
bolega
Guest
Posts: n/a
 
      01-14-2011
Basically, I have spent a few hours experimenting and searching on the
comp.lang.c/c++

Let me use SQR for brevity and saving line

Here are progressively refined macros :

#define SQR(x) ((x)*(x))

#define SQR(x) ({typedef xtype=x; xtype xval=x; xval*xval}) // NOTE,
closure or {} inside () is a valid idea in C, and thus no return is
needed.

this macro is given in several posts like

http://groups.google.com/group/comp....xtype%22&pli=1

there is a problem with typedef

Bolega
 
Reply With Quote
 
 
 
 
luser- -droog
Guest
Posts: n/a
 
      01-14-2011
On Jan 14, 12:46*am, bolega <(E-Mail Removed)> wrote:
> Basically, I have spent a few hours experimenting and searching on the
> comp.lang.c/c++
>
> Let me use SQR for brevity and saving line
>
> Here are progressively refined macros :
>
> #define SQR(x) ((x)*(x))
>
> #define SQR(x) ({typedef xtype=x; xtype xval=x; xval*xval}) *// NOTE,
> closure or {} inside () is a valid idea in C, and thus no return is
> needed.
>
> this macro is given in several posts like
>
> http://groups.google.com/group/comp....m/thread/38ef2...
>
> there is a problem with typedef
>
> Bolega


What problem?
 
Reply With Quote
 
 
 
 
Rivka Miller
Guest
Posts: n/a
 
      01-14-2011
On Jan 13, 10:46*pm, bolega <(E-Mail Removed)> wrote:
> Basically, I have spent a few hours experimenting and searching on the
> comp.lang.c/c++
>
> Let me use SQR for brevity and saving line
>
> Here are progressively refined macros :
>
> #define SQR(x) ((x)*(x))
>
> #define SQR(x) ({typedef xtype=x; xtype xval=x; xval*xval}) *// NOTE,
> closure or {} inside () is a valid idea in C, and thus no return is
> needed.
>
> this macro is given in several posts like
>
> http://groups.google.com/group/comp....m/thread/38ef2...
>
> there is a problem with typedef
>
> Bolega


try if this works

#define square(x) ({typeof(x) temp=x; temp*temp;})

Rivka
 
Reply With Quote
 
luser- -droog
Guest
Posts: n/a
 
      01-14-2011
On Jan 14, 12:46*am, bolega <(E-Mail Removed)> wrote:
> Basically, I have spent a few hours experimenting and searching on the
> comp.lang.c/c++
>
> Let me use SQR for brevity and saving line
>
> Here are progressively refined macros :
>
> #define SQR(x) ((x)*(x))
>
> #define SQR(x) ({typedef xtype=x; xtype xval=x; xval*xval}) *// NOTE,
> closure or {} inside () is a valid idea in C, and thus no return is
> needed.
>
> this macro is given in several posts like
>
> http://groups.google.com/group/comp....m/thread/38ef2...
>
> there is a problem with typedef
>
> Bolega


OIK

#define SQR(x) pow((x),2)
or
#define SQR(x) exp(2 * log(x))
 
Reply With Quote
 
Hans Vlems
Guest
Posts: n/a
 
      01-14-2011
On 14 jan, 08:15, luser- -droog <(E-Mail Removed)> wrote:
> On Jan 14, 12:46*am, bolega <(E-Mail Removed)> wrote:
>
>
>
>
>
> > Basically, I have spent a few hours experimenting and searching on the
> > comp.lang.c/c++

>
> > Let me use SQR for brevity and saving line

>
> > Here are progressively refined macros :

>
> > #define SQR(x) ((x)*(x))

>
> > #define SQR(x) ({typedef xtype=x; xtype xval=x; xval*xval}) *// NOTE,
> > closure or {} inside () is a valid idea in C, and thus no return is
> > needed.

>
> > this macro is given in several posts like

>
> >http://groups.google.com/group/comp....m/thread/38ef2...

>
> > there is a problem with typedef

>
> > Bolega

>
> OIK
>
> #define SQR(x) pow((x),2)
> or
> #define SQR(x) exp(2 * log(x))- Tekst uit oorspronkelijk bericht niet weergeven -
>
> - Tekst uit oorspronkelijk bericht weergeven -


What happens for x=0 in the second example?
 
Reply With Quote
 
Gert-Jan de Vos
Guest
Posts: n/a
 
      01-14-2011
On Jan 14, 7:46*am, bolega <(E-Mail Removed)> wrote:
> Basically, I have spent a few hours experimenting and searching on the
> comp.lang.c/c++
>
> Let me use SQR for brevity and saving line
>
> Here are progressively refined macros :
>
> #define SQR(x) ((x)*(x))
>
> #define SQR(x) ({typedef xtype=x; xtype xval=x; xval*xval}) *// NOTE,
> closure or {} inside () is a valid idea in C, and thus no return is
> needed.
>
> this macro is given in several posts like
>
> http://groups.google.com/group/comp....m/thread/38ef2...
>
> there is a problem with typedef
>
> Bolega


Why not just an inline function template (this being c.l.c++)?

template <typename T>
inline T square(T value)
{
return value*value;
}

Gert-Jan
 
Reply With Quote
 
luser- -droog
Guest
Posts: n/a
 
      01-14-2011
On Jan 14, 2:14*am, Hans Vlems <(E-Mail Removed)> wrote:
> On 14 jan, 08:15, luser- -droog <(E-Mail Removed)> wrote:
>
>
>
> > On Jan 14, 12:46*am, bolega <(E-Mail Removed)> wrote:

>
> > > Basically, I have spent a few hours experimenting and searching on the
> > > comp.lang.c/c++

>
> > > Let me use SQR for brevity and saving line

>
> > > Here are progressively refined macros :

>
> > > #define SQR(x) ((x)*(x))

>
> > > #define SQR(x) ({typedef xtype=x; xtype xval=x; xval*xval}) *// NOTE,
> > > closure or {} inside () is a valid idea in C, and thus no return is
> > > needed.

>
> > > this macro is given in several posts like

>
> > >http://groups.google.com/group/comp....m/thread/38ef2....

>
> > > there is a problem with typedef

>
> > > Bolega

>
> > OIK

>
> > #define SQR(x) pow((x),2)
> > or
> > #define SQR(x) exp(2 * log(x))- Tekst uit oorspronkelijk bericht niet weergeven -

>
> > - Tekst uit oorspronkelijk bericht weergeven -

>
> What happens for x=0 in the second example?


Rats! And you can't do a ternary 'cause you'd have to
mention x twice.

But that would be a penalty in lisp, too wouldn't it?
An expression with a side-effect would perform the
side-effect twice, right?
 
Reply With Quote
 
Bart van Ingen Schenau
Guest
Posts: n/a
 
      01-14-2011
On Jan 14, 7:46*am, bolega <(E-Mail Removed)> wrote:
> Basically, I have spent a few hours experimenting and searching on the
> comp.lang.c/c++
>
> Let me use SQR for brevity and saving line
>
> Here are progressively refined macros :
>
> #define SQR(x) ((x)*(x))
>
> #define SQR(x) ({typedef xtype=x; xtype xval=x; xval*xval}) *// NOTE,
> closure or {} inside () is a valid idea in C, and thus no return is
> needed.


The ({ ... }) construct is not valid in standard C or standard C++. It
is a GCC extension.
If you want a standard-conforming macro that works for any type and
also yields a value of that same type, you are stuck with the first
macro you present.
There is no way to write such a macro in standard C without evaluating
x twice.

In C++, you have the option of using an inline templated function, as
shown by Gert-Jan de Vos.

If your portability needs do not extend beyond GCC (and compilers
accepting the same dialect), you can use an expression-block similar
to the second block, as shown by Rivka Miller.

>
> Bolega


Bart v Ingen Schenau
 
Reply With Quote
 
James Kuyper
Guest
Posts: n/a
 
      01-14-2011
On 01/14/2011 03:51 AM, Gert-Jan de Vos wrote:
> On Jan 14, 7:46�am, bolega<(E-Mail Removed)> wrote:
>> Basically, I have spent a few hours experimenting and searching on the
>> comp.lang.c/c++
>>
>> Let me use SQR for brevity and saving line
>>
>> Here are progressively refined macros :
>>
>> #define SQR(x) ((x)*(x))
>>
>> #define SQR(x) ({typedef xtype=x; xtype xval=x; xval*xval}) �// NOTE,
>> closure or {} inside () is a valid idea in C, and thus no return is
>> needed.
>>
>> this macro is given in several posts like
>>
>> http://groups.google.com/group/comp....m/thread/38ef2...
>>
>> there is a problem with typedef
>>
>> Bolega

>
> Why not just an inline function template (this being c.l.c++)?
>
> template<typename T>
> inline T square(T value)
> {
> return value*value;
> }


Because this is also cross-posted to c.l.c. That could mean he's one of
those people who thinks C and C++ are a single language called C/C++,
but alternatively, it could mean that he needs a separate solution for
each language, or a single solution that works in both languages.

 
Reply With Quote
 
Stefan Monnier
Guest
Posts: n/a
 
      01-14-2011
> Because this is also cross-posted to c.l.c. That could mean he's one of
> those people who thinks C and C++ are a single language called C/C++, but
> alternatively, it could mean that he needs a separate solution for each
> language, or a single solution that works in both languages.


Since he also cross-posted to g.e.help, I guess we should presume he
thinks Help is also a language in the same family or that he also wants
a solution in that language?


Stefan
 
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
GURU NEEDED : macro SQUARE(x) for any type x bolega C Programming 12 01-15-2011 07:55 PM
IT Job Guru - Certification Guru Rohit A+ Certification 0 08-13-2008 07:31 PM
any charitable hmtl guru needed!! cassiadenton@gmail.com HTML 31 12-07-2006 08:22 AM
501 PIX "deny any any" "allow any any" Any Anybody? Networking Student Cisco 4 11-16-2006 10:40 PM
Language and Direction real GURU needed! (Hebrew and maybe arabic) CR or Charset issue? Efy. ASP .Net 2 06-25-2003 03:28 PM



Advertisments