POV-Ray : Newsgroups : povray.pov4.discussion.general : I want to be rid of my stupid mistakes Server Time
29 Jun 2024 06:04:05 EDT (-0400)
  I want to be rid of my stupid mistakes (Message 4 to 13 of 53)  
<<< Previous 3 Messages Goto Latest 10 Messages Next 10 Messages >>>
From: Trevor G Quayle
Subject: Re: I want to be rid of my stupid mistakes
Date: 20 Jul 2009 13:25:00
Message: <web.4a647edf97aca27181c811d20@news.povray.org>
"bgimeno" <bgimeno[at]persistencia[dot]org> wrote:
> The following code is perfectly valid to the present version, but does not
> produce anything.
>
> #declare FooCount = 0 ;
> #while (FooCount < 100 )
> sphere {<0,FooCount,0>,1 pigment { rgb< 1, 1, 0> } }
> // #declare FooCount = FooCount + 1 ; // note this missing line
> #end
>
> I suppose that a "detector" of infinite loops is it too much to ask?
>
> B. Gimeno

When I write a counting loop, I tend to do it as follows:

#declare FooCount = 0 ; #while (FooCount < 100 )
   > BLAH BLAH BLAH
#declare FooCount = FooCount + 1 ; #end

I know there are circumstances where this can't be done, but I find it helps me
recognize my "for" type loops easily.

-tgq


Post a reply to this message

From: bgimeno
Subject: Re: I want to be rid of my stupid mistakes
Date: 20 Jul 2009 15:01:59
Message: <4a64bf27$1@news.povray.org>
I don't know if there is a special case in which a loop of  the variable 
"FooCounter" should remain unchanged.
But what about a simple detector where the value of "FooCounter" from the 
#while(FooCounter cond) condition does not change when you reach the end of 
the loop?.

If this is an "insurmountable" logical hole guess we will have to settle for 
what is, but thanks anyway for your reply.

B. Gimeno


Post a reply to this message

From: Warp
Subject: Re: I want to be rid of my stupid mistakes
Date: 20 Jul 2009 15:49:40
Message: <4a64ca54@news.povray.org>
bgimeno <bgimeno[at]persistencia[dot]org> wrote:
> I don't know if there is a special case in which a loop of  the variable 
> "FooCounter" should remain unchanged.
> But what about a simple detector where the value of "FooCounter" from the 
> #while(FooCounter cond) condition does not change when you reach the end of 
> the loop?.

  Just because it didn't change in one loop doesn't mean it will never
change.

-- 
                                                          - Warp


Post a reply to this message

From: Reactor
Subject: Re: I want to be rid of my stupid mistakes
Date: 20 Jul 2009 16:25:01
Message: <web.4a64d1cd97aca2713227537b0@news.povray.org>
"bgimeno" <bgimeno[at]persistencia[dot]org> wrote:
> I don't know if there is a special case in which a loop of  the variable
> "FooCounter" should remain unchanged.
> But what about a simple detector where the value of "FooCounter" from the
> #while(FooCounter cond) condition does not change when you reach the end of
> the loop?.
>
> If this is an "insurmountable" logical hole guess we will have to settle for
> what is, but thanks anyway for your reply.
>
> B. Gimeno

Actually, I just wrote some code that has that.  The loop counter is
conditionally incremented, and the condition isn't known until after it is
evaluated.  I cut out some earlier code that wasn't relevant (mostly file
writing stuff).

#local i = 0;
#local seaweedCount = 10000;

#while( i < seaweedCount )
    #local xcord = xdist * rand(rs1) + bounds1.x;
    #local zcord = zdist * rand(rs1) + bounds1.z;

    #local normVect = <0,0,0>;
    #local intersectVect = trace(o_rocks,<xcord,10,zcord>, <0.5,-1,1>,
normVect);

    #if( intersectVect.y < waterLevel + 0.5 )
        sphere
        {
            intersectVect, 0.05
            texture{ marker1s }
        }
        #local i = i + 1;

    #end
#end

Basically it selects a random point within the bounds of a heightfield, then
uses trace to find the exact point on the heightfield.  It then checks to make
sure the point is close to the water level.  If the point isn't slightly above
the water level or lower, the counter is not incremented.  In this way I get
seaweedCount (10000) objects where I need them, and not 10000 total attempts to
place an object, whether it is successful or not.

It is quite possible to place a secondary condition so that it "bails out" after
some very high number of tries, but only the individual user can decide for a
given instance of where and what that condition should be.

-Reactor


Post a reply to this message

From: Aydan
Subject: Re: I want to be rid of my stupid mistakes
Date: 21 Jul 2009 06:30:01
Message: <web.4a65984397aca2711ccf29180@news.povray.org>
It's not really an infinite loop condition detector but a "I forgot to increment
my variable"-detector:

Check if the loop condition variable is being reassigned inside the loop.


Post a reply to this message

From: Warp
Subject: Re: I want to be rid of my stupid mistakes
Date: 21 Jul 2009 06:35:29
Message: <4a6599f1@news.povray.org>
Aydan <hes### [at] hendrik-sachsenet> wrote:
> Check if the loop condition variable is being reassigned inside the loop.

  Impossible in the general case.

  Besides, you are assuming that there is a single unambiguous "loop condition
variable". The condition in the #while construct can be *anything*. It can be
a complex expression, it can be a function or macro call, it can contain a
condition composed of many variables...

  For example, if you had something like this:

#while(xCoord * 2.5 < yCoord * 5 & sqrt(zCoord) < 10 * zSize)

how would POV-Ray check that this isn't an infinite loop?

-- 
                                                          - Warp


Post a reply to this message

From: scott
Subject: Re: I want to be rid of my stupid mistakes
Date: 21 Jul 2009 06:53:42
Message: <4a659e36@news.povray.org>
>> Check if the loop condition variable is being reassigned inside the loop.
>
>  Impossible in the general case.
>
>  Besides, you are assuming that there is a single unambiguous "loop 
> condition
> variable". The condition in the #while construct can be *anything*. It can 
> be
> a complex expression, it can be a function or macro call, it can contain a
> condition composed of many variables...
>
>  For example, if you had something like this:
>
> #while(xCoord * 2.5 < yCoord * 5 & sqrt(zCoord) < 10 * zSize)
>
> how would POV-Ray check that this isn't an infinite loop?

Impossible to catch all infinite loops, but the parser could check to make 
sure at least one of the variables mentioned in the #while condition is 
reassigned within the loop.  That would catch a lot of mistakes (eg 
forgetting to increment the index variable).


Post a reply to this message

From: clipka
Subject: Re: I want to be rid of my stupid mistakes
Date: 21 Jul 2009 07:50:01
Message: <web.4a65aac997aca271537313280@news.povray.org>
"bgimeno" <bgimeno[at]persistencia[dot]org> wrote:
> I don't know if there is a special case in which a loop of  the variable
> "FooCounter" should remain unchanged.
> But what about a simple detector where the value of "FooCounter" from the
> #while(FooCounter cond) condition does not change when you reach the end of
> the loop?.

First of all, this would require special handling for conditional expressions to
detect the presence of such a construct during parsing.

Furthermore, consider this:

  #declare R = seed(4711);
  #declare Items = 0;
  #while (Items < 10000)
    #declare MyPos = SomeRandomPosition(R);
    #if (SomeCondition(MyPos))
      DoSomething(MyPos)
      #declare Items = Items + 1;
    #end
  #end

Obviously, the problem is more complicated than just checking whether the
variable value actually changes in each loop: Runtime checking won't do, you'd
need code analysis. Also note that the increment may be hidden nested deeply in
macros, so this is not a straightforward check for the existence of a "#declare
FooCounter = ..." somewhere inside the loop.

In my eyes it would be a kludge anyway, so why not heal the leg instead, and add
a proper #for loop - I'm sure we'd have that issue solved then.

(If POV 4 does indeed get a new SDL as promised, I'm quite sure it will not come
without a for loop anyway.)


Post a reply to this message

From: Warp
Subject: Re: I want to be rid of my stupid mistakes
Date: 21 Jul 2009 08:01:51
Message: <4a65ae2f@news.povray.org>
scott <sco### [at] scottcom> wrote:
> Impossible to catch all infinite loops, but the parser could check to make 
> sure at least one of the variables mentioned in the #while condition is 
> reassigned within the loop.  That would catch a lot of mistakes (eg 
> forgetting to increment the index variable).

  What if it's not incremented within the loop? Maybe it's incremented
elsewhere (eg. inside a macro called from the loop).

-- 
                                                          - Warp


Post a reply to this message

From: clipka
Subject: Re: I want to be rid of my stupid mistakes
Date: 21 Jul 2009 08:20:00
Message: <web.4a65b21097aca271537313280@news.povray.org>
"scott" <sco### [at] scottcom> wrote:
> Impossible to catch all infinite loops, but the parser could check to make
> sure at least one of the variables mentioned in the #while condition is
> reassigned within the loop.  That would catch a lot of mistakes (eg
> forgetting to increment the index variable).

What if we're talking about a loop like this:

  #declare abortProb = 0.03;
  #while(rand(R) > abortProb)
    //...
  #end

Still insist on the variable being re-assigned? What if not rand() is used, but
some macro creating a non-uniform distribution?

What if no variable is used at all, as in:

  #while(rand(R) > 0.03)
    //...
  #end


I do know I keep repeating myself, but: Why on earth try to come up with
ever-so-complex ways to detect a broken counting #while loop without breaking
other uses of the #while statement, when really the root problem is the absence
of a proper #for loop?

Virtually no-one is asking for an infinite loop detection in other languages
like Pascal, C, VB or what-have-you. Why? Because they do have a for-loop so
you usually don't encounter this blunder there.


Post a reply to this message

<<< Previous 3 Messages Goto Latest 10 Messages Next 10 Messages >>>

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