POV-Ray : Newsgroups : povray.general : Problems with iterations Server Time
27 Nov 2024 22:45:41 EST (-0500)
  Problems with iterations (Message 1 to 10 of 16)  
Goto Latest 10 Messages Next 6 Messages >>>
From: SharkD
Subject: Problems with iterations
Date: 10 Jan 2008 01:20:01
Message: <web.4785b89a636bc06df6ab856c0@news.povray.org>
Check out this test scene:

#local i = 0;
#local numerator = 1;
#local divisor = 6;
#while(i < numerator)
 #local i = i + numerator/divisor;
 #debug concat("i = ", str(i, 0, -1),"\n")
#end

Shouldn't the iterations stop when i = 1? What am I doing wrong?


Post a reply to this message

From: SharkD
Subject: Re: Problems with iterations
Date: 10 Jan 2008 02:25:00
Message: <web.4785c7acf1dc5f73f6ab856c0@news.povray.org>
"SharkD" <nomail@nomail> wrote:
> Check out this test scene:
>
> #local i = 0;
> #local numerator = 1;
> #local divisor = 6;
> #while(i < numerator)
>  #local i = i + numerator/divisor;
>  #debug concat("i = ", str(i, 0, -1),"\n")
> #end
>
> Shouldn't the iterations stop when i = 1? What am I doing wrong?

Just to be clear, the last entry printed to the Messages panel is "i =
1.166667".


Post a reply to this message

From: Fredrik Eriksson
Subject: Re: Problems with iterations
Date: 10 Jan 2008 03:02:17
Message: <op.t4pe93otcs6ysw@e6600.bredbandsbolaget.se>
On Thu, 10 Jan 2008 08:22:20 +0100, SharkD <nomail@nomail> wrote:
> "SharkD" <nomail@nomail> wrote:
>> Check out this test scene:
>>
>> #local i = 0;
>> #local numerator = 1;
>> #local divisor = 6;
>> #while(i < numerator)
>>  #local i = i + numerator/divisor;
>>  #debug concat("i = ", str(i, 0, -1),"\n")
>> #end
>>
>> Shouldn't the iterations stop when i = 1? What am I doing wrong?
>
> Just to be clear, the last entry printed to the Messages panel is "i =

> 1.166667".


You should move the variable update (i.e. "#local i = i +...") to the 
end  

of the loop. You should also realize that comparing floating point numbe
rs  

like this is problematic at best, and should generally be avoided.



-- 

FE


Post a reply to this message

From: SharkD
Subject: Re: Problems with iterations
Date: 10 Jan 2008 03:45:01
Message: <web.4785da57f1dc5f73f6ab856c0@news.povray.org>
"Fredrik Eriksson" <noo### [at] nowherecom> wrote:
> On Thu, 10 Jan 2008 08:22:20 +0100, SharkD <nomail@nomail> wrote:
> > "SharkD" <nomail@nomail> wrote:
> >> Check out this test scene:
> >>
> >> #local i = 0;
> >> #local numerator = 1;
> >> #local divisor = 6;
> >> #while(i < numerator)
> >>  #local i = i + numerator/divisor;
> >>  #debug concat("i = ", str(i, 0, -1),"\n")
> >> #end
> >>
> >> Shouldn't the iterations stop when i = 1? What am I doing wrong?
> >
> > Just to be clear, the last entry printed to the Messages panel is "i =
>
> > 1.166667".
>
>
> You should move the variable update (i.e. "#local i = i +...") to the
> end
>
> of the loop. You should also realize that comparing floating point numbe
> rs
>
> like this is problematic at best, and should generally be avoided.
>
>
>
> --
>
> FE

If I move it to the end it will merely say "i = 1.000000". The condition still
should have failed in this case. I.e., the loop should not run past 0.833333.


Post a reply to this message

From: SharkD
Subject: Re: Problems with iterations
Date: 10 Jan 2008 04:00:00
Message: <web.4785ddf4f1dc5f73f6ab856c0@news.povray.org>
"Fredrik Eriksson" <noo### [at] nowherecom> wrote:
> On Thu, 10 Jan 2008 08:22:20 +0100, SharkD <nomail@nomail> wrote:
> > "SharkD" <nomail@nomail> wrote:
> >> Check out this test scene:
> >>
> >> #local i = 0;
> >> #local numerator = 1;
> >> #local divisor = 6;
> >> #while(i < numerator)
> >>  #local i = i + numerator/divisor;
> >>  #debug concat("i = ", str(i, 0, -1),"\n")
> >> #end
> >>
> >> Shouldn't the iterations stop when i = 1? What am I doing wrong?
> >
> > Just to be clear, the last entry printed to the Messages panel is "i =
>
> > 1.166667".
>
>
> You should move the variable update (i.e. "#local i = i +...") to the
> end
>
> of the loop. You should also realize that comparing floating point numbe
> rs
>
> like this is problematic at best, and should generally be avoided.
>
>
>
> --
>
> FE

I moved the incrementor to the end of the loop as you suggested:

#local i = 0;
#local numerator = 1;
#local divisor = 6;
#while(i < numerator)
 #debug concat("i = ", str(i, 0, -1),"\n")
 #local i = i + numerator/divisor;
#end

The result, however, lists seven iterations:

i = 0.000000
i = 0.166667
i = 0.333333
i = 0.500000
i = 0.666667
i = 0.833333
i = 1.000000

There should only be six!


Post a reply to this message

From: scott
Subject: Re: Problems with iterations
Date: 10 Jan 2008 04:04:44
Message: <4785dfac$1@news.povray.org>
> The result, however, lists seven iterations:
>
> i = 0.000000
> i = 0.166667
> i = 0.333333
> i = 0.500000
> i = 0.666667
> i = 0.833333
> i = 1.000000
>
> There should only be six!

I wouldn't mind betting that 1/6 cannot be exactly represented by a floating 
point number.  So by the time you have summed 6 of them you actually have 
0.9999999999999999999999 stored and not 1.0000000000000000 as you expect. 
The output only shows 1.00000 because it is rounded.

You shouldn't rely on floating point numbers being exact like you are doing, 
find another way to do it.

This goes for any programming language using the normal floating point data 
types by the way, not just POV.


Post a reply to this message

From: Warp
Subject: Re: Problems with iterations
Date: 10 Jan 2008 06:33:25
Message: <47860285@news.povray.org>
SharkD <nomail@nomail> wrote:
> There should only be six!

  You have found the inaccuracies of floating point numbers.

  If you want an exact number of iterations, use integers, and at the
beginning of the loop, calculate the actual value you want to use from
that integer loop counter.

  For example, assume you want a loop with a variable which goes from
0 to 1 at steps of 0.1, and stops before it reaches the 1. The correct
way to do that is:

#declare Index = 0;
#while(Index < 10)
  #declare Value = Index/10;

  ... (the body of the loop here, which uses 'Value') ...

  #declare Index = Index+1;
#end

  This would be the *wrong* way of doing it:

#declare Value = 0;
#while(Value < 1)
  ...
  #declare Value = Value + 0.1;
#end

-- 
                                                          - Warp


Post a reply to this message

From: John VanSickle
Subject: Re: Problems with iterations
Date: 10 Jan 2008 15:46:21
Message: <4786841d$1@news.povray.org>
SharkD wrote:
> "SharkD" <nomail@nomail> wrote:
>> Check out this test scene:
>>
>> #local i = 0;
>> #local numerator = 1;
>> #local divisor = 6;
>> #while(i < numerator)
>>  #local i = i + numerator/divisor;
>>  #debug concat("i = ", str(i, 0, -1),"\n")
>> #end
>>
>> Shouldn't the iterations stop when i = 1? What am I doing wrong?
> 
> Just to be clear, the last entry printed to the Messages panel is "i =
> 1.166667".

Change it to this:

#local i = 0;
#local numerator = 1;
#local divisor = 6;
#while(i < numerator)
   #debug concat("i = ", str(i, 0, -1),"\n")
   #local i = i + numerator/divisor;
#end

and it will end before reporting i>=0.

Regards,
John


Post a reply to this message

From: SharkD
Subject: Re: Problems with iterations
Date: 10 Jan 2008 16:05:00
Message: <web.478687b0f1dc5f73d410b38f0@news.povray.org>
Warp <war### [at] tagpovrayorg> wrote:
> SharkD <nomail@nomail> wrote:
> > There should only be six!
>
>   You have found the inaccuracies of floating point numbers.
>
>   If you want an exact number of iterations, use integers, and at the
> beginning of the loop, calculate the actual value you want to use from
> that integer loop counter.
>
>   For example, assume you want a loop with a variable which goes from
> 0 to 1 at steps of 0.1, and stops before it reaches the 1. The correct
> way to do that is:
>
> #declare Index = 0;
> #while(Index < 10)
>   #declare Value = Index/10;
>
>   ... (the body of the loop here, which uses 'Value') ...
>
>   #declare Index = Index+1;
> #end
>
>   This would be the *wrong* way of doing it:
>
> #declare Value = 0;
> #while(Value < 1)
>   ...
>   #declare Value = Value + 0.1;
> #end
>
> --
>                                                           - Warp

I'm wondering if simply changing the condition to "i < 0.999999" is sufficient.
The likelihood of the variable falling outside this range is pretty slim. Also,
to how many decimal places does POV-Ray calculate? I thought the "system
specific" option in the "str" function returned values using this amount.


Post a reply to this message

From: Nicolas Alvarez
Subject: Re: Problems with iterations
Date: 10 Jan 2008 16:17:35
Message: <47868b6f$1@news.povray.org>

> I'm wondering if simply changing the condition to "i < 0.999999" is sufficient.
> The likelihood of the variable falling outside this range is pretty slim. Also,
> to how many decimal places does POV-Ray calculate? I thought the "system
> specific" option in the "str" function returned values using this amount.

"To how many decimal places does POV-Ray calculate" is the wrong 
question to ask. POV-Ray asks your CPU to do floating-point 
calculations, that's all. POV-Ray doesn't do numerical calculations in 
any different way than the rest of your computer.

Also, floating-point numbers don't even work in terms of "decimal places".

Use this:

#local i = 0;
#local numerator = 1;
#local divisor = 6;
#while(i < divisor)
  #local x = i*numerator/divisor;
  #debug concat("x = ", str(x, 0, -1),"\n")
  #local i = i + 1;
#end

and be done with it.


Post a reply to this message

Goto Latest 10 Messages Next 6 Messages >>>

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