macro computations

This is a discussion on macro computations within the C++ Programming forums, part of the General Programming Boards category; 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 ...

  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,185
    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, 10:13 AM
  2. Errors including <windows.h>
    By jw232 in forum Windows Programming
    Replies: 4
    Last Post: 07-29-2008, 02:29 PM
  3. Quantum Random Bit Generator
    By shawnt in forum C++ Programming
    Replies: 62
    Last Post: 06-18-2008, 11:17 AM
  4. Macro Program
    By daspope in forum Windows Programming
    Replies: 5
    Last Post: 04-25-2004, 05:02 AM
  5. about Makefile and Macro
    By tom_mk in forum C++ Programming
    Replies: 1
    Last Post: 09-18-2003, 02:07 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21