POV-Ray : Newsgroups : povray.general : arrays.inc - Rand_Array_Item() trouble Server Time
1 Aug 2024 06:20:52 EDT (-0400)
  arrays.inc - Rand_Array_Item() trouble (Message 1 to 8 of 8)  
From: How Camp
Subject: arrays.inc - Rand_Array_Item() trouble
Date: 12 Apr 2006 13:30:00
Message: <web.443d3793c13db847429e56f30@news.povray.org>
I'm sure this is something silly.  Test1 works fine, but the line just prior
to Test2 complains that I don't have a ';' at the end of my #declare
statement
(which I do).  What am I doing wrong?  Can anyone give a simple working
example of Rand_Array_Item() for me?


#include "arrays.inc"
#declare MyArray = array[5] {0, 1, 2, 3, 4};
#declare RS = seed(12345);
#debug concat("Test1: ", str(Rand_Array_Item(MyArray,RS),0,0), "\n")
#declare Test = Rand_Array_Item(MyArray,RS);
#debug concat("Test2: ", str(Test,0,0), "\n")


- How


Post a reply to this message

From: Chris B
Subject: Re: arrays.inc - Rand_Array_Item() trouble
Date: 12 Apr 2006 14:56:06
Message: <443d4d46$1@news.povray.org>
"How Camp" <hac### [at] gmailcom> wrote in message 
news:web.443d3793c13db847429e56f30@news.povray.org...
>
> I'm sure this is something silly.  Test1 works fine, but the line just 
> prior
> to Test2 complains that I don't have a ';' at the end of my #declare
> statement
> (which I do).  What am I doing wrong?  Can anyone give a simple working
> example of Rand_Array_Item() for me?
>
>
> #include "arrays.inc"
> #declare MyArray = array[5] {0, 1, 2, 3, 4};
> #declare RS = seed(12345);
> #debug concat("Test1: ", str(Rand_Array_Item(MyArray,RS),0,0), "\n")
> #declare Test = Rand_Array_Item(MyArray,RS);
> #debug concat("Test2: ", str(Test,0,0), "\n")
>
>
> - How
>

Odd!...

If you convert the result returned from the macro call into a string before 
assigning it to the variable it works.
Also if you copy the active ingredients from the Rand_Array_Item macro into 
the source it works.
But if you assign the numeric result from the macro to a variable without 
converting it to a string it complains.
Anyway, all of the following examples work.
Maybe someone else could offer an explanation as to why the one you have 
doesn't, but I can't see any material difference.


#include "arrays.inc"

#declare MyArray = array[5] {0, 1, 2, 3, 4};
#declare RS = seed(12345);

#debug concat("Test1: ", str(Rand_Array_Item(MyArray,RS),0,0), "\n")

#declare Test = str(Rand_Array_Item(MyArray,RS),0,0);
#debug concat("Test2: ", Test, "\n")

#declare Test = 
MyArray[floor(rand(RS)*0.9999999*dimension_size(MyArray,1))];
#debug concat("Test3: ", str(Test,0,0), "\n")

Regards,
Chris B.


Post a reply to this message

From: Trevor G Quayle
Subject: Re: arrays.inc - Rand_Array_Item() trouble
Date: 12 Apr 2006 15:35:00
Message: <web.443d560fde047fad6c4803960@news.povray.org>
Hmm, the problem seems to be as follows:
declares of floats need to be followed by ';'

  #declare A=1;

1) placing the ';' outside an #if block doesn't work, the #else or #end is
encountered before the semicolon is found
  #declare B= #if(A=1) 12 #else 13 #end; ---> doesn't work

2) placing the ';' inside an #if block does work
  #declare B= #if(A=1) 12; #else 13; #end ---> works

essentially what is happening is event 1), the macro contents replace the
macro call, thus the semicolon gets placed outside the #if block and
generates an error.  When placing this inside the str() or concat()
functions, the semicolon is not required insdie the function and it parses
fine.

In order to get it to work, you will either need to make you own macro or
modify the one in arrays.inc and remove the #if block (it's only there as
an error check so it isn't absolutely necessary):

#macro Rand_Array_Item(Array, Stream)
   //remove   #if(dimensions(Array)=1)
      Array[floor(rand(Stream)*0.9999999*dimension_size(Array,1))]
   //remove   #else
   //remove      #error "The Rand_Array_Item() macro only works for 1D
arrays."
   //remove   #end
#end

becomes:

#macro Rand_Array_Item(Array, Stream)
      Array[floor(rand(Stream)*0.9999999*dimension_size(Array,1))]
#end

Also: adding the semicolon to the macro isn't the best solution as then
functions like str() and concat() won't work with it

also, the macro could be rearranged to keep the error check as:

#macro Rand_Array_Item(Array, Stream)
   #if(dimensions(Array)!=1)
      #error "The Rand_Array_Item() macro only works for 1D arrays."
   #end
    Array[floor(rand(Stream)*0.9999999*dimension_size(Array,1))]
#end

-tgq


Post a reply to this message

From: Chris B
Subject: Re: arrays.inc - Rand_Array_Item() trouble
Date: 12 Apr 2006 15:59:08
Message: <443d5c0c$1@news.povray.org>
"Trevor G Quayle" <Tin### [at] hotmailcom> wrote in message 
news:web.443d560fde047fad6c4803960@news.povray.org...
> Hmm, the problem seems to be as follows:
> declares of floats need to be followed by ';'
>
>  #declare A=1;
>
> 1) placing the ';' outside an #if block doesn't work, the #else or #end is
> encountered before the semicolon is found
>  #declare B= #if(A=1) 12 #else 13 #end; ---> doesn't work
>
> 2) placing the ';' inside an #if block does work
>  #declare B= #if(A=1) 12; #else 13; #end ---> works
>
> essentially what is happening is event 1), the macro contents replace the
> macro call, thus the semicolon gets placed outside the #if block and
> generates an error.  When placing this inside the str() or concat()
> functions, the semicolon is not required insdie the function and it parses
> fine.

Hi Trevor,

Prompted by your findings I tried just putting the macro call inside 
brackets and that seems to work too:

#declare Test = (Rand_Array_Item(MyArray,RS));
#debug concat("Test2: ", str(Test,0,0), "\n")

Regards,
Chris B.


Post a reply to this message

From: Warp
Subject: Re: arrays.inc - Rand_Array_Item() trouble
Date: 12 Apr 2006 16:07:09
Message: <443d5dec@news.povray.org>
Chris B <c_b### [at] btconnectcomnospam> wrote:
> Prompted by your findings I tried just putting the macro call inside 
> brackets and that seems to work too:

  Perhaps the macro itself should be modified so that it has the
parentheses...

-- 
                                                          - Warp


Post a reply to this message

From: Chris B
Subject: Re: arrays.inc - Rand_Array_Item() trouble
Date: 12 Apr 2006 16:34:42
Message: <443d6462$1@news.povray.org>
"Warp" <war### [at] tagpovrayorg> wrote in message 
news:443d5dec@news.povray.org...
> Chris B <c_b### [at] btconnectcomnospam> wrote:
>> Prompted by your findings I tried just putting the macro call inside
>> brackets and that seems to work too:
>
>  Perhaps the macro itself should be modified so that it has the
> parentheses...
>
> -- 
>                                                          - Warp

I've just tried that, but it then doesn't work with strings.

Regards,
Chris B.


Post a reply to this message

From: How Camp
Subject: Re: arrays.inc - Rand_Array_Item() trouble
Date: 13 Apr 2006 07:05:00
Message: <web.443e2ff1de047fad429e56f30@news.povray.org>
"Trevor G Quayle" <Tin### [at] hotmailcom> wrote:

> essentially what is happening is event 1), the macro contents replace the
> macro call, thus the semicolon gets placed outside the #if block and
> generates an error.  When placing this inside the str() or concat()
> functions, the semicolon is not required inside the function and it parses
> fine.


Thanks, Trevor (and everyone else).  This makes sense, now.  For a while, I
thought I was losing my sanity...

- How


Post a reply to this message

From: ingo
Subject: Re: arrays.inc - Rand_Array_Item() trouble
Date: 13 Apr 2006 12:06:06
Message: <Xns97A4B8241CED2seed7@news.povray.org>
in news:web.443d560fde047fad6c4803960@news.povray.org Trevor G Quayle 
wrote:

> also, the macro could be rearranged to keep the error check as:
> 

#macro Rand_Array_Item(Array, Stream)
   #if(dimensions(Array)=1)
      #local Return = Array[floor(rand(Stream)*0.9999999*dimension_size
(Array,1))];
   #else
      #error "The Rand_Array_Item() macro only works for 1D arrays."
   #end
   Return
#end

I've ran so often into this problem that I use the above more or less as 
a standard solution now.

I wouldn't mind if a #return statement became part of the SDL in a 
(near) future version. And for the error handling a #try #except #else 
#finally would also be nice for a (not so near) future version.

Ingo


Post a reply to this message

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