Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C++ (http://www.velocityreviews.com/forums/f39-c.html)
-   -   GURU NEEDED : macro SQUARE(x) for any type x (http://www.velocityreviews.com/forums/t742015-guru-needed-macro-square-x-for-any-type-x.html)

bolega 01-14-2011 06:46 AM

GURU NEEDED : macro SQUARE(x) for any type x
 
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

luser- -droog 01-14-2011 07:09 AM

Re: GURU NEEDED : macro SQUARE(x) for any type x
 
On Jan 14, 12:46*am, bolega <gnuist...@gmail.com> 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?

Rivka Miller 01-14-2011 07:10 AM

Re: GURU NEEDED : macro SQUARE(x) for any type x
 
On Jan 13, 10:46*pm, bolega <gnuist...@gmail.com> 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

luser- -droog 01-14-2011 07:15 AM

Re: GURU NEEDED : macro SQUARE(x) for any type x
 
On Jan 14, 12:46*am, bolega <gnuist...@gmail.com> 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))

Hans Vlems 01-14-2011 08:14 AM

Re: GURU NEEDED : macro SQUARE(x) for any type x
 
On 14 jan, 08:15, luser- -droog <mijo...@yahoo.com> wrote:
> On Jan 14, 12:46*am, bolega <gnuist...@gmail.com> 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?

Gert-Jan de Vos 01-14-2011 08:51 AM

Re: GURU NEEDED : macro SQUARE(x) for any type x
 
On Jan 14, 7:46*am, bolega <gnuist...@gmail.com> 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

luser- -droog 01-14-2011 09:04 AM

Re: GURU NEEDED : macro SQUARE(x) for any type x
 
On Jan 14, 2:14*am, Hans Vlems <hvl...@freenet.de> wrote:
> On 14 jan, 08:15, luser- -droog <mijo...@yahoo.com> wrote:
>
>
>
> > On Jan 14, 12:46*am, bolega <gnuist...@gmail.com> 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?

Bart van Ingen Schenau 01-14-2011 09:57 AM

Re: GURU NEEDED : macro SQUARE(x) for any type x
 
On Jan 14, 7:46*am, bolega <gnuist...@gmail.com> 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

James Kuyper 01-14-2011 11:13 AM

Re: GURU NEEDED : macro SQUARE(x) for any type x
 
On 01/14/2011 03:51 AM, Gert-Jan de Vos wrote:
> On Jan 14, 7:46�am, bolega<gnuist...@gmail.com> 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.


Stefan Monnier 01-14-2011 08:01 PM

Re: GURU NEEDED : macro SQUARE(x) for any type x
 
> 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


All times are GMT. The time now is 02:14 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.