|
 |
kurtz le pirate <kur### [at] free fr> wrote:
> Hello,
>
>
> I just spent a long, long time debugging my code.
>
> For example, this macro :
>
> #macro binomial_coefficient(n, k)
>
> #local result = 0.0;
>
> #if ( k<0 | k>n)
> #local result = 0.0;
>
> #elseif (k=0 | k=n)
> #local result = 1.0;
>
> #else
> #local result = 1.0;
> #if ( k > n-k )
> #local k = n - k;
> #end
> #local i = 0;
> #while ( i < k )
> #local result = result*(n-i);
> #local result = result/(i+1);
> #local i = i + 1;
> #end
> #end
>
> // return scalar value
> result
> #end
>
>
>
> At first glance, nothing special, but there is a problem, a big problem,
> that I hadn't noticed :(
>
>
> Can you see it too? Well done.
>
>
> I also program in C, so perhaps that's where the confusion comes from.
> But I'll explain it anyway.
>
>
> The annoying part is this one :
>
> #if ( k > n-k )
> #local k = n - k;
> #end
>
> ... and so, the "#local k =" led me to believe I was using a local
> variable. Well, not at all. "k" represents the variable passed as a
> parameter and its value is modified in the part that called this macro.
>
> By simplifying :
>
> #declare n = 5;
> #declare k = 3;
> #declare r = binomial_coefficient(n,k);
> ...and here k = 2, not 3 !
>
>
> Therefore, be careful not to use a macro parameter in an assignment.
>
> Do you agree with me, or have I misunderstood something again?
> --
> kurtz le pirate
> compagnie de la banquise
I agree! I program in C++ and POV macros and variables are very different!
Try this macro.
#macro ts(R)
#local R=sphere{0,1 pigment{Gold}}
#end
#declare P=5; ts(P)
object{P}
Have Fun!
Post a reply to this message
|
 |