Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > C macro

Reply
Thread Tools

C macro

 
 
janus
Guest
Posts: n/a
 
      01-26-2013
Hello All,

I am currently reading Lua source. I find the following macro ,
#define l_tg(x) (x) used in this form
static int math_abs (lua_State *L) {
lua_pushnumber(L, l_tg(fabs)(luaL_checknumber(L, 1)));
return 1;
}
What is the real reason for macro l_tag(XXX)?

http://www.lua.org/source/5.2/lmathlib.c.html

At times I see stuff like this #define CAP_POSIT . Without value or any other thing attached to it, why this?
 
Reply With Quote
 
 
 
 
Ike Naar
Guest
Posts: n/a
 
      01-26-2013
On 2013-01-26, janus <(E-Mail Removed)> wrote:
> Hello All,
>
> I am currently reading Lua source. I find the following macro ,
> #define l_tg(x) (x) used in this form
> static int math_abs (lua_State *L) {
> lua_pushnumber(L, l_tg(fabs)(luaL_checknumber(L, 1)));
> return 1;
> }
> What is the real reason for macro l_tag(XXX)?
>
> http://www.lua.org/source/5.2/lmathlib.c.html


In that document there is a comment near the definition of l_tg:

macro 'l_tg' allows the addition of an 'l' or 'f' to all math operation

By redefining l_tg, one can use the 'float' or 'long double' versions of
all math functions in one blow. For instance, one could redefine

#define l_tg(x) (x##f)

and then l_tg(cos) would expand to (cosf), l_tg(atan2) to (atan2f), etc.

> At times I see stuff like this #define CAP_POSIT . Without value or any
> other thing attached to it, why this?


This is convenient for macros that can have only two values, say on/off.
One value is represented by the macro being defined, the other
by the macro not being defined.

For example,

#define CAP_POSIT
/* ... */
#ifdef CAP_POSIT
/* stuff that applies to CAP_POSIT */
#endif

vs.

#undef CAP_POSIT
/* ... */
#ifdef CAP_POSIT
/* stuff that applies to CAP_POSIT */
#endif

In the first case, the 'stuff that applies to CAP_POSIT' is selected,
in the second case it isn't.
 
Reply With Quote
 
 
 
 
janus
Guest
Posts: n/a
 
      01-26-2013

Thanks, however, I am still confused.

To me these two are different

#define l_tg(x) (x)

and

#define l_tg(b) (bXXf)
I very much understand this one. It is concatenation, but the there first macro lacked that ability.



On Saturday, January 26, 2013 6:42:59 AM UTC+1, janus wrote:
> Hello All,
>
>
>
> I am currently reading Lua source. I find the following macro ,
>
> #define l_tg(x) (x) used in this form
>
> static int math_abs (lua_State *L) {
>
> lua_pushnumber(L, l_tg(fabs)(luaL_checknumber(L, 1)));
>
> return 1;
>
> }
>
> What is the real reason for macro l_tag(XXX)?
>
>
>
> http://www.lua.org/source/5.2/lmathlib.c.html
>
>
>
> At times I see stuff like this #define CAP_POSIT . Without value or any other thing attached to it, why this?




On Saturday, January 26, 2013 6:42:59 AM UTC+1, janus wrote:
> Hello All,
>
>
>
> I am currently reading Lua source. I find the following macro ,
>
> #define l_tg(x) (x) used in this form
>
> static int math_abs (lua_State *L) {
>
> lua_pushnumber(L, l_tg(fabs)(luaL_checknumber(L, 1)));
>
> return 1;
>
> }
>
> What is the real reason for macro l_tag(XXX)?
>
>
>
> http://www.lua.org/source/5.2/lmathlib.c.html
>
>
>
> At times I see stuff like this #define CAP_POSIT . Without value or any other thing attached to it, why this?

 
Reply With Quote
 
Ike Naar
Guest
Posts: n/a
 
      01-26-2013
On 2013-01-26, janus <(E-Mail Removed)> wrote:
>
> Thanks, however, I am still confused.
>
> To me these two are different
>
> #define l_tg(x) (x)
>
> and
>
> #define l_tg(b) (bXXf)


(By the way, why do you write 'XX' for '##' ?)

> I very much understand this one. It is concatenation, but the there
> first macro lacked that ability.


Because the first macro does not need that ability.
With the first macro, l_tg(atan2) translates to (atan2),
a function that works with doubles.
That is probably what you want if you use the first macro.

With the second macro, l_tg(atan2) translates to (atan2f),
a function that works with floats.
which is probably what you want if you use the second macro.

If you wanted to use functions that work with long doubles,
you would have used yet another macro:

#define l_tg(x) (x##l)

and l_tg(atan2) would translate to (atan2l)
 
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
Dedicated Macro or Normal Macro? John Ortt Digital Photography 5 11-22-2005 12:43 PM
Macro lens on a camera with a macro setting??? mitchell.chris@gmail.com Digital Photography 2 09-28-2005 07:55 AM
in S.E. Asia : Canon EOS 300d with 100 macro ED vs. Nikon D70 with Nikon 105 macro ? J. Cod Digital Photography 0 09-29-2004 05:46 AM
#define macro to enclose an older macro with strings Dead RAM C++ 20 07-14-2004 10:58 AM
macro name from macro? D Senthil Kumar C Programming 1 09-21-2003 07:02 PM



Advertisments