POV-Ray : Newsgroups : povray.general : macro always returns zero Server Time
24 Apr 2024 09:00:22 EDT (-0400)
  macro always returns zero (Message 1 to 9 of 9)  
From: stevenvh
Subject: macro always returns zero
Date: 25 May 2017 09:05:00
Message: <web.5926d605d6da163dddeb13060@news.povray.org>
I have written this macro to generate integers in [0..A[

 #macro RandInt(A, RS)  floor(A * rand(RS) * 0.999999)  #end

The factor 0.999999 is to ensure A itself is excluded; rand() returns a value in
[0..1], so including 1.

When I use #debug to see the macro's return value it's fine, but when I assign
it to a variable, like

 #declare somevar = RandInt(4, Rnd3);

somevar will always be 0. Can anybody explain this, and how to solve it? I've
written several similar macros which work fine.

TIA
Steven


Post a reply to this message

From: clipka
Subject: Re: macro always returns zero
Date: 25 May 2017 09:31:48
Message: <5926dcc4$1@news.povray.org>
Am 25.05.2017 um 15:03 schrieb stevenvh:
> I have written this macro to generate integers in [0..A[
> 
>  #macro RandInt(A, RS)  floor(A * rand(RS) * 0.999999)  #end
> 
> The factor 0.999999 is to ensure A itself is excluded; rand() returns a value in
> [0..1], so including 1.
> 
> When I use #debug to see the macro's return value it's fine, but when I assign
> it to a variable, like
> 
>  #declare somevar = RandInt(4, Rnd3);
> 
> somevar will always be 0. Can anybody explain this, and how to solve it? I've
> written several similar macros which work fine.

Looks to me like your problem is somewhere else. Maybe a minimal
self-contained scene file demonstrating the problem would help.


Post a reply to this message

From: Anthony D  Baye
Subject: Re: macro always returns zero
Date: 25 May 2017 11:20:00
Message: <web.5926f58a2c479cc1fd6b6fe10@news.povray.org>
"stevenvh" <nomail@nomail> wrote:
> I have written this macro to generate integers in [0..A[
>
>  #macro RandInt(A, RS)  floor(A * rand(RS) * 0.999999)  #end
>
> The factor 0.999999 is to ensure A itself is excluded; rand() returns a value in
> [0..1], so including 1.
>
> When I use #debug to see the macro's return value it's fine, but when I assign
> it to a variable, like
>
>  #declare somevar = RandInt(4, Rnd3);
>
> somevar will always be 0. Can anybody explain this, and how to solve it? I've
> written several similar macros which work fine.
>
> TIA
> Steven

I suspect that the problem comes from not reseeding Rand(...) Every time the
macro is called. I'M ve run into this problem myself.

These days, I always use Rand_Normal(...) From rand.inc

Regards,
A.D.B.


Post a reply to this message

From: stevenvh
Subject: Re: macro always returns zero
Date: 25 May 2017 11:25:00
Message: <web.5926f6e12c479cc1ddeb13060@news.povray.org>
clipka <ano### [at] anonymousorg> wrote:
>
> Looks to me like your problem is somewhere else. Maybe a minimal
> self-contained scene file demonstrating the problem would help.

OK, so I stripped everything unnecessary for creating some output and saved it
as test.pov. What do you think? It works, of course :-(.
I then removed code line by line from my original file until it would work. By
the way, "work" means it doesn't go in an infinite parsing loop.
The code contains

#declare colorRnd1 = 0;
#declare colorRnd2 = 0;
#while ( colorRnd2 = colorRnd1 )
  #declare colorRnd1 = RandInt(4, Rnd3);
  #declare colorRnd2 = RandInt(4, Rnd3);
#end

and since RandInt always seems to return zero it never exits the while loop.

In the end I had a file which still didn't work and an identical-looking one
which did work. Since the problem seemed to be with the RandInt I scrutinized
the above lines. I compared them character by character (esp. the digits!). And
did so 5 (five) times. They were identical in both files.
If there was any difference it was not visible as ASCII text. So I copied the
above code from the working test.pov and pasted it in the other file.
Result: the latter also works.

I regret that I didn't compare the files with a hex editor. I believe there was
one or more erroneous characters in the file which confused the parser.
This happened to me before! A file which looked syntactically and semantically
OK caused parsing errors. After spending an eternity checking the syntax I
finally put the cursor some 40 or 50 characters after what should have been the
end of the line, and backspaced until the last non-blank character. Problem
solved.

I'm using the POVray for Windows editor component; possibly the cause of the
problem lies there. (I'm not very fond of PovClipse. Eclipse is written in Java,
and Java is infamous for being a huge security liability.)


Post a reply to this message

From: Bald Eagle
Subject: Re: macro always returns zero
Date: 25 May 2017 12:00:01
Message: <web.5926fe582c479cc1c437ac910@news.povray.org>
Where are you defining the value for Rnd3 ?


Post a reply to this message

From: stevenvh
Subject: Re: macro always returns zero
Date: 25 May 2017 12:10:00
Message: <web.592700b12c479cc1ddeb13060@news.povray.org>
"Bald Eagle" <cre### [at] netscapenet> wrote:
> Where are you defining the value for Rnd3 ?

In my OP I said that #debug showed that non-zero values were generated, so that
wasn't the problem.

In my previous post I explained that the .pov file's text seemed to have been
corrupted with non-visible characters.

Thanks for your time, though.


Post a reply to this message

From: clipka
Subject: Re: macro always returns zero
Date: 25 May 2017 13:33:17
Message: <5927155d$1@news.povray.org>
Am 25.05.2017 um 17:17 schrieb Anthony D. Baye:

> I suspect that the problem comes from not reseeding Rand(...) Every time the
> macro is called. I'M ve run into this problem myself.

Uh... the general recommended practice is to *NOT* reseed. Otherwise you
*WILL* get the same result each time you call the macro.


Post a reply to this message

From: scott
Subject: Re: macro always returns zero
Date: 26 May 2017 08:55:21
Message: <592825b9@news.povray.org>
> In my previous post I explained that the .pov file's text seemed to have been
> corrupted with non-visible characters.
>
> Thanks for your time, though.

It would still be worth posting the scene file that *doesn't* work (as 
attachments rather than copy&paste), just so others can see what's going 
on and how to avoid in future. I've never had this problem before, how 
do you think you got the strange characters in there? Were you entering 
special characters or copying&pasting from another source?


Post a reply to this message

From: Bald Eagle
Subject: Re: macro always returns zero
Date: 22 Jun 2017 15:20:01
Message: <web.594c17832c479cc1c437ac910@news.povray.org>
just came across this, so someone has noticed the same type of problem (though
not in povray)


http://abel.math.harvard.edu/~knill/technology/index.html


23-10-2015: While Mimic is funny, there is a serious side. I use demononizer
since more than a decade (practically in any script which processes content from
a website) as Microsoft has in their "time of evil" completely ruined things. We
have got used to it, but it still happens to me regularly that a string
formatted of Microsoft (or Adobe) origin (especially " or - symbols) ruined a
program from running. There is nothing more frustrating than to have two texts
which look identical in the editor, where one works, the other not and where
only watching the file with a binhex editor reveals the hidden difference.


Post a reply to this message

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