cancel
Showing results forย 
Search instead forย 
Did you mean:ย 
EN
cancel
Showing results forย 
Search instead forย 
Did you mean:ย 
Anonymous
Not applicable

? on For Next Loops

222: !BRICK TIES

FOR dist=0 TO 30' STEP brick_tie
ADDx dist
GOSUB 111

NEXT dist

RETURN

Will this work in an object? It's just putting 1 in and that's it. The brick_tie is a parameter for a length. GOSUB 111 is the actual lines for the brick tie.

Thanks for any help,
Michele
17 Replies 17
Anonymous
Not applicable
Thanks Tom.
I know someone who has a strong computer science background
and I will ask him some of these questions and maybe he will
have some answers.
Thanks,
Peter Devlin
Oleg
Expert
Peter wrote:
Hello Tom,
Does the C++ for loop have the "step" part like GDL ?
Peter Devlin
C++ can use steps of course and more.
A problem may arise for both FOR and WHILE, as result of roundings and comparing floating numbers. ( GDL, C++,...).
Both variants above are IMHO almost equivalent and not quite correct.

IMHO better
FOR dist=0 TO 30 ' + brick_tie / 2 STEP brick_tie
Anonymous
Not applicable
Hello Oleg,
Great to hear from you.
You say," Both variants above are IMHO almost equivalent
and not quite correct."
Could you elaborate a little please.
How are they both not quite correct ?
Thank you,
Peter Devlin
Oleg
Expert
Peter wrote:
Hello Oleg,
Could you elaborate a little please.
How are they both not quite correct ?
Thank you,
Peter Devlin
Well. Some tests ( AC9 )
What do you think will printed

counter = 0
bstep = 3
FOR dist=0 TO 3 STEP bstep
  counter = counter + 1
NEXT dist
PRINT counter
Imagine there was some calculations.
bstep is same, but what will be printed now

counter = 0
bstep = 0.3/0.1
FOR dist=0 TO 3 STEP bstep
  counter = counter + 1
NEXT dist
PRINT counter
Same for WHILE

counter = 0
bstep = 0.3
dist = 0
WHILE dist < 0.3 DO 
  dist = dist + bstep
  counter = counter + 1
ENDWHILE
PRINT counter

counter = 0
bstep = 1 - 0.7
dist = 0
WHILE dist < 0.3 DO 
  dist = dist + bstep
  counter = counter + 1
ENDWHILE
PRINT counter
The result will depend on actual values and calculations that's mean unstable.

Oleg
Anonymous
Not applicable
Hello Oleg,
Thank you for your considerable elaboration.
I will have to copy your code into a lib part and
get my head around what is happening.
Thank you,
Peter Devlin
Anonymous
Not applicable
Hello Oleg,
I did an experiment with your second piece of code.

counter = 0
bstep = 0.3/0.1 !!!=3
FOR dist=0 TO 3 STEP bstep
counter = counter + 1
NEXT dist
PRINT counter !!!! prints 1

PRINT 0.3/0.1 !!!! prints 3

When I change the code to this.

counter = 0
bstep = int(0.3/0.1) !!!=3
FOR dist=0 TO 3 STEP bstep
counter = counter + 1
NEXT dist
PRINT counter !!!! prints 2

It is as though there is a secret fractional part
of the calculation of 0.3/0.1 that is not displayed
in what is printed in PRINT 0.3/0.1 and that there
is a rounding-up in the internal calculations.

How can this be ???
Is PRINT able to lie ?
I wonder how many places PRINT can display.

Peter Devlin
Oleg
Expert
PRINT does not lie. It rounds number to some signs before display ( it seems to 6 signs ).

In any case it needs to be ready, that issues may arise at any comparisons of floating numbers. ( like 0.3 is not equal 1-0.7 )
And if it important in script, use some tricks, like ABS, EPS etc
( It seems there is some guide from GS about that )

FOR loop uses internally comparison too and it may results to unexpected results. Therefore, when a loop uses floating numbers, and not integer, better to use it like
FOR dist=0 TO 3 + bstep / 2 STEP bstep

Oleg
Anonymous
Not applicable
Hello Oleg,
Yes, this is something that I have only recently come to understand.
Now, all I need to do is remember to put in that little extra to
help AC make it to the finish line.
Thanks Oleg,
Peter Devlin

Didn't find the answer?

Check other topics in this Forum

Back to Forum

Read the latest accepted solutions!

Accepted Solutions

Start a new conversation!