POV-Ray : Newsgroups : povray.general : Help creating a sgn() function : Re: Help creating a sgn() function Server Time
2 Aug 2024 00:16:11 EDT (-0400)
  Re: Help creating a sgn() function  
From: kurtz le pirate
Date: 4 Mar 2005 02:44:56
Message: <422811f8$1@news.povray.org>

news:42274012$1@news.povray.org...
> Hey Folks,
>    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.
>
> First I tried with a function:
>
> #declare sgn = function(xx) { #if (xx<0) -1 #else #if (xx>0) 1 #else 0
> #end #end }
>
> But this dies with a "expected 'numeric expression', undeclared
> identifier 'xx' found instead". I take this to mean you can't use a if
> inside a function.
>
> So I tried with a macro:
>
> #macro sgn(xx)
>    #if (xx < 0)
>      -1
>    #else
>      #if (xx > 0)
>        1
>      #else
>        0
>      #end
>    #end
> #end
>
> But this gives me a Parse Error: Expected ')', # found instead on the
> "-1" line.
>
> Am I just being really dumb? This should be a no-brainer...
>
> Cheers,
>

    and this : #declare sgn=function(xx){xx/abs(xx)} ... of course be
careful
    when xx = 0.


Post a reply to this message

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