Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C Programming (http://www.velocityreviews.com/forums/f42-c-programming.html)
-   -   C macro (http://www.velocityreviews.com/forums/t956954-c-macro.html)

 janus 01-26-2013 05:42 AM

C macro

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?

 Ike Naar 01-26-2013 06:33 AM

Re: C macro

On 2013-01-26, janus <emekamicro@gmail.com> 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.

 janus 01-26-2013 07:16 AM

Re: C macro

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?

 Ike Naar 01-26-2013 07:36 AM

Re: C macro

On 2013-01-26, janus <emekamicro@gmail.com> 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)

 All times are GMT. The time now is 08:46 AM.