Thread: macro computations

  1. #1
    Registered User
    Join Date
    Jul 2004
    Posts
    222

    macro computations

    I want to use a macro definition to implement a simple conversion utility as shown here

    Code:
    #define MM_TO_POINT(mm)								2.83464567*mm
    Do numerical macros store integers by default? If yes, would I need to have a type cast to a double/floating point type and rounding to achieve what I want here?

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    It's a macro -- all it does is take every occurrence of "MM_TO_POINT(bob)" and turn it into "2.83464567*bob". C will later interpret 2.83464567 as a double-precision number. But it doesn't "return" anything, it's just an expression.

  3. #3
    The larch
    Join Date
    May 2006
    Posts
    3,573
    What?

    Macros are nothing more than find-and-replace rules that are done before the compiler sees the code.

    This is how some example usages would be replaced:
    Code:
    MM_TO_POINT(23) --> 2.83464567*23
    x = MM_TO_POINT(y); --> x = 2.83464567*y;
    Now a more "interesting" case:
    Code:
    x = MM_TO_POINT(a + 5); --> x = 2.83464567*a + 5;
    (This is why macro "arguments" should always be in extra brackets.)

    However, macro's are a very dumb tool. Why not use a (template) function to achieve the exact same result?
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem building Quake source
    By Silvercord in forum Game Programming
    Replies: 16
    Last Post: 07-11-2010, 09:13 AM
  2. Errors including <windows.h>
    By jw232 in forum Windows Programming
    Replies: 4
    Last Post: 07-29-2008, 01:29 PM
  3. Quantum Random Bit Generator
    By shawnt in forum C++ Programming
    Replies: 62
    Last Post: 06-18-2008, 10:17 AM
  4. Macro Program
    By daspope in forum Windows Programming
    Replies: 5
    Last Post: 04-25-2004, 04:02 AM
  5. about Makefile and Macro
    By tom_mk in forum C++ Programming
    Replies: 1
    Last Post: 09-18-2003, 01:07 PM