C macro

J

janus

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

Ike Naar

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

janus

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.



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

Ike Naar

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)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top