|
![](/i/fill.gif) |
> I'm trying to create a sgn() function, like in BASIC. Where sgn(x) =
> -1 if x < 0, +1 if x > 0 and 0 if x = 0.
#declare sign = function (x) {select(x,-1,0,1)};
> #declare sgn = function(xx) { #if (xx<0) -1 #else #if (xx>0) 1 #else 0
> #end #end }
Anything that belongs with # is a parse-time command which is evaluated to
*create* the function. The function arguments such as xx are not available
at this time because the function doesn't yet exist. Control structures in a
function can only be created via functions like select().
> So I tried with a macro:
>
> #macro sgn(xx)
> #if (xx < 0)
> -1
> #else
> #if (xx > 0)
> 1
> #else
> 0
> #end
> #end
> #end
When returning values with a macro, use #local to declare the return value
and then put the variable at the end:
#macro sgn(xx)
#if (xx < 0)
#local result = -1;
#else
#if (xx > 0)
#local result = 1;
#else
#local result = 0;
#end
#end
result
#end
- Slime
[ http://www.slimeland.com/ ]
Post a reply to this message
|
![](/i/fill.gif) |