|
|
|
|
|
|
| |
| |
|
|
|
|
| |
| |
|
|
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
|
|
| |
| |
|
|
|
|
| |
| |
|
|
"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
|
|
| |
| |
|
|
|
|
| |
| |
|
|
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
|
|
| |
| |
|
|
|
|
| |
| |
|
|
"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
|
|
| |
| |
|
|
|
|
| |
| |
|
|
"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
|
|
| |
| |
|
|
|
|
| |
| |
|
|
> 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
|
|
| |
| |
|
|
|
|
| |
| |
|
|
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
|
|
| |
| |
|
|
|
|
| |
| |
|
|
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
|
|
| |
| |
|
|
|
|
| |
| |
|
|
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
|
|
| |
| |
|
|
|
|
| |
| |
|
|
> 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
|
|
| |
| |
|
|
|
|
| |