 |
 |
|
 |
|
 |
|  |
|  |
|
 |
|
 |
|  |
|  |
|
 |
Nicolas Alvarez wrote:
> The problem with doing #declare V=V+1/100; is that it might not reach
> 1.0000 exactly at the end of the loop (because of floating point
> inaccuracies), so it might run one time too many.
Or even if it runs runs the correct number of times, it may have slightly
off values. And errors would *accumulate*.
Tested in Python:
>>> x = 0;
>>> for i in range(0,100):
... x += 1.0/100;
...
>>> print x;
1.0000000000000007
>>>
Post a reply to this message
|
 |
|  |
|  |
|
 |
|
 |
|  |
|  |
|
 |
Nicolas Alvarez wrote:
> Nicolas Alvarez wrote:
>> The problem with doing #declare V=V+1/100; is that it might not reach
>> 1.0000 exactly at the end of the loop (because of floating point
>> inaccuracies), so it might run one time too many.
>
> Or even if it runs runs the correct number of times, it may have slightly
> off values. And errors would *accumulate*.
>
> Tested in Python:
>>>> x = 0;
>>>> for i in range(0,100):
> ... x += 1.0/100;
> ...
>>>> print x;
> 1.0000000000000007
I see what you mean. I've never encountered any unwanted effects from
using fractional number-dependent loops, but it's a good thing to keep
in mind. Errors at the 16th decimal place are acceptable for most of the
things I do, but there will probably come a time when your input saves
me from beating my head against a hard surface :)
Sam
Post a reply to this message
|
 |
|  |
|  |
|
 |
|
 |
|  |
|  |
|
 |
Nicolas Alvarez wrote:
> The problem with doing #declare V=V+1/100; is that it might not reach 1.0000
> exactly at the end of the loop (because of floating point inaccuracies), so
> it might run one time too many.
>
Right. If you MUST use floats, more robust is this sort of thing:
#local xmin = ....;
#local xmax = ....;
#local dx = .....;
#local x = xmin;
#while (x < xmax + dx / 2)
#local x = x + dx;
#end
Post a reply to this message
|
 |
|  |
|  |
|
 |
|
 |
|  |
|  |
|
 |
stbenge wrote in message <49027f8c@news.povray.org>:
> but since
> POV-Ray treats all variables as floats, I can't see how it could harm
> things.
You mean, instead of using an integer counter? There is a big difference:
with most architectures, floats can represent reasonably-small integers in
an exact manner. On the other hand, 1/100 is not an exact float; 1/64 and
1/128 would have been, but not 1/100.
So the point that PoV internally uses floats is not valid: there are more
robust ways to use floats than others, and using an integer counter for
looks is one.
Post a reply to this message
|
 |
|  |
|  |
|
 |
|
 |
|  |
|  |
|
 |
stbenge wrote:
> I see what you mean. I've never encountered any unwanted effects from
> using fractional number-dependent loops, but it's a good thing to keep
> in mind. Errors at the 16th decimal place are acceptable for most of the
> things I do, but there will probably come a time when your input saves
> me from beating my head against a hard surface :)
It's mainly relevant if you loop thousands of times. With 100 iterations
it's not a big problem :)
Post a reply to this message
|
 |
|  |
|  |
|
 |
|
 |
|  |
|  |
|
 |
Nicolas Alvarez wrote in message <4903699f@news.povray.org>:
> It's mainly relevant if you loop thousands of times. With 100 iterations
> it's not a big problem :)
Quite the contrary, with this code, there may be problems even for a very
small number of iterations.
For example, consider the following code:
#local n = 10;
#local t = 0;
#while(t < 1)
#local t = t + 1/n;
#end
and the same with n = 11. It happens that the float value for 1/10 is
slightly smaller than the exact value 1/10, while the float value for 1/11
is slightly greater than the exact value for 1/11. The result is that both
loops make 11 iterations.
This issue can be fixed by using a float-friendly bound, as suggested
earlier by someone: t < 1 + dt / 2 or t < 1 - dt / 2, depending on whether
the loop is supposed to be inclusive or not.
Post a reply to this message
|
 |
|  |
|  |
|
 |
|
 |
|  |
|  |
|
 |
Nicolas George wrote:
> For example, consider the following code:
>
> #local n = 10;
> #local t = 0;
> #while(t < 1)
> #local t = t + 1/n;
> #end
>
Well, t's a a reserved identifier, but otherwise....
If you change it count from 0 to "< 10" in increments of 1 (ie in integers), it's
fine.
Dan
Post a reply to this message
|
 |
|  |
|  |
|
 |
|
 |
|  |
|  |
|
 |
Dan Connelly wrote:
> If you change it count from 0 to "< 10" in increments of 1 (ie in
> integers), it's fine.
Yes, that's exactly the point. Use integers instead of floats.
Post a reply to this message
|
 |
|  |
|  |
|
 |
|
 |
|  |
|  |
|
 |
stbenge <THI### [at] hotmail com> wrote:
> #declare native_motif=
> pigment{
> gradient y triangle_wave
> #declare V=0;
> #while(V<1)
> translate y*.75
> rotate z*45.2
> //rotate -z*45.1
> scale .98
> warp{repeat x*5 flip x}
> #declare V=V+1/100;
> #end
> scale .1
> }
I just want to say this is so much fun to play around with. Thanks for sharing
it.
Janet
Post a reply to this message
|
 |
|  |
|  |
|
 |
|
 |
|  |
|  |
|
 |
"Janet" <par### [at] att net> wrote:
>
> I just want to say this is so much fun to play around with. Thanks for sharing
> it.
>
My mother would have given her eye teeth for a pattern generator like this. I
can see her using it for Fair Isle jumpers.
Stephen
Post a reply to this message
|
 |
|  |
|  |
|
 |
|
 |
|  |