POV-Ray : Newsgroups : povray.advanced-users : Local variables in a function : Re: Local variables in a function Server Time
6 Oct 2024 13:39:01 EDT (-0400)
  Re: Local variables in a function  
From: Mike Williams
Date: 3 Jul 2006 07:38:14
Message: <CnbTmDA7DQqEFwYX@econym.demon.co.uk>
Wasn't it bugman who wrote:
>I know how to create local variables in a macro, but after extensive
>searching, I still have not found any way to create local variables in a
>function. Is this even possible?
>
>Just for a crazy example, suppose I had something like this:
>#declare f=function(x) {sum(i,1,1000,pow(sin(x),i))}
>
>Then it would be much faster if I could make a local variable, say,
>something like this:
>#declare f=function(x) {sinx=sin(x); sum(i,1,1000,pow(sinx,i))}

Within a function, you can't set the value of a variable

If you're using this particular function to produce results for
individual values of x, rather than using it for an isosurface or
pigment where x varies beyond the reach of the SDL, then you could pass
the variable as a parameter:

#declare f=function(sinx) {sum(i,1,10000000,pow(sinx,i))}  
#debug str( f(sin(0.1)),-1,-1 )

However, it turns out to only be 13% faster than writing 

#declare f=function(x) {sum(i,1,10000000,pow(sin(x),i))}
#debug str(f(0.1),-1,-1)

It looks like evaluating sin(x) is incredibly efficient in POV
functions.

-- 
Mike Williams
Gentleman of Leisure


Post a reply to this message

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