#macro and #if

General FreeBASIC programming questions.
Post Reply
Schrubber
Posts: 50
Joined: Dec 07, 2009 11:09

#macro and #if

Post by Schrubber »

Hello,

Why does this not go?

Code: Select all


#macro machwas(DTYPE, D)

#if typeof(DTYPE) = typeof(single)
    cast(single, D)
#endif

#endmacro

    dim as double cr = 0.0045
    dim as single e

    e = machwas(single, cr)

Thanks
Schrubber
SARG
Posts: 1888
Joined: May 27, 2005 7:15
Location: FRANCE

Re: #macro and #if

Post by SARG »

#if and so on are to be used with the preprocessor not for your code.

You should use if and endif but think that the macro is replaced by all its lines giving a strange statement and obviously errors....

Why not simply write ?

Code: Select all

e = cast(single, cr)
Schrubber
Posts: 50
Joined: Dec 07, 2009 11:09

Re: #macro and #if

Post by Schrubber »

Hello,

these were only one simplistic examples.

Code: Select all

#macro machwas(DTYPE, D)

#if typeof(DTYPE) = typeof(DOUBLE)
  iif(abs(cast(DTYPE, iif(cast(DTYPE, D) >= 0, cast(DTYPE, D), cast(DTYPE, -D)))) <= 0.000000000001, true, false)
#elseif typeof(DTYPE) = typeof(SINGLE)
  iif(abs(cast(DTYPE, iif(cast(DTYPE, D) >= 0, cast(DTYPE, D), cast(DTYPE, -D)))) <= 0.00001f, true, false)
#endif

#endmacro

this is what lies behind.

Thanks
counting_pine
Site Admin
Posts: 6323
Joined: Jul 05, 2005 17:32
Location: Manchester, Lancs

Re: #macro and #if

Post by counting_pine »

It doesn't work because the #if's are evaluated when the macros are expanded, and preprocessor directives can't come in the middle of the line.
Your macro is expanded as something like:

Code: Select all

e =
#if typeof(single) = typeof(single)
    cast(single, D)
#endif
One workaround is to build the assignment into the macro:

Code: Select all

#macro machwas(e, DTYPE, D)

#if typeof(DTYPE) = typeof(single)
    e = cast(single, D)
#endif

#endmacro

    dim as double cr = 0.0045
    dim as single e

    machwas(e, single, cr)
Schrubber
Posts: 50
Joined: Dec 07, 2009 11:09

Re: #macro and #if

Post by Schrubber »

Hy,

thanks @SARG, @counting_pine

I will implement the junk as a function.

Gruß
Schrubber
Post Reply