POV-Ray : Newsgroups : povray.general : Help creating a sgn() function : Re: Help creating a sgn() function Server Time
2 Aug 2024 00:19:09 EDT (-0400)
  Re: Help creating a sgn() function  
From: Slime
Date: 3 Mar 2005 12:04:59
Message: <422743bb$1@news.povray.org>
>    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

Copyright 2003-2023 Persistence of Vision Raytracer Pty. Ltd.