C macro

Discussion in 'C Programming' started by janus, Jan 26, 2013.

  1. janus

    janus Guest

    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?
     
    janus, Jan 26, 2013
    #1
    1. Advertising

  2. janus

    Ike Naar Guest

    On 2013-01-26, 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


    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.
     
    Ike Naar, Jan 26, 2013
    #2
    1. Advertising

  3. janus

    janus Guest

    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?
     
    janus, Jan 26, 2013
    #3
  4. janus

    Ike Naar Guest

    On 2013-01-26, janus <> 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)
     
    Ike Naar, Jan 26, 2013
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Dead RAM
    Replies:
    20
    Views:
    1,132
    John Harrison
    Jul 14, 2004
  2. D Senthil Kumar

    macro name from macro?

    D Senthil Kumar, Sep 20, 2003, in forum: C Programming
    Replies:
    1
    Views:
    590
    Jack Klein
    Sep 21, 2003
  3. sounak

    to get macro name from macro value

    sounak, Nov 22, 2005, in forum: C Programming
    Replies:
    17
    Views:
    512
    Mark McIntyre
    Nov 22, 2005
  4. Patrick Kowalzick
    Replies:
    5
    Views:
    485
    Patrick Kowalzick
    Mar 14, 2006
  5. Mike Manilone

    macro inside macro

    Mike Manilone, Oct 3, 2011, in forum: C Programming
    Replies:
    8
    Views:
    474
    Mike Manilone
    Oct 6, 2011
Loading...

Share This Page