This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
Goof's profile picture

why are my Goto program, moving a bit wrong? (1-4 spaces wrong)

Started by Goof, 06 February 2013 - 09:27 AM
Goof #1
Posted 06 February 2013 - 10:27 AM
Hi.

i've made a Goto program which uses my GPS system..

first i placed my turtle. then i ran the "gps locate" to see if the GPS works… and it was correct… the chordinates worked fine…
but. when i then ran my program, and told it to go to the specific chordinates, it is going some blocks to far.. can anyone show me why?

code:

Pastebin



two links ( they are same )

Thanks in advance!!
Djerun #2
Posted 06 February 2013 - 12:42 PM
in your while runUp > 0 and while runDown > 0 loops you don't decrement runUp or runDown
also if your turtle couldn't move forward into that space why should it be able from the top or the bottom?


if your turtle needs to go two blocks down it will get stuck going down and up again

if your turtle needs to go up or down and can't while digIsPossible == false it will be stuck in a loop without yielding which can cause the program to abort


say x is 1 and gox is 2 then your turtle would go in a for i=1,2 loop which means it's executed twice and your turtle would have moved 1 too far
Goof #3
Posted 06 February 2013 - 07:52 PM
Will test it out…
Goof #4
Posted 06 February 2013 - 08:22 PM
ehm, so i actually have to add in a "-1" after the for i = 1,2 -1 ???

im not sure, but when i tested it yesterday it does everything too short.
Doyle3694 #5
Posted 06 February 2013 - 09:15 PM
for i = 1,2,-1 do wont even run because it knows it will never end
theoriginalbit #6
Posted 06 February 2013 - 09:39 PM
for i = 1,2 do wont even run because it knows it will never end
ummmm………..

for i = 1, 2 do
  print(i)
end
will run exactly twice. printing 1 and 2.

this is what would not run.

for i = 1, 2, -1 do
  print(i)
end
Goof #7
Posted 06 February 2013 - 09:50 PM
but… i actually found it out.. when i used the for i = 1,2 -1 it actually worked.

(cur code)
Doyle3694 #8
Posted 07 February 2013 - 01:01 AM
for i = 1,2 do wont even run because it knows it will never end
ummmm………..

for i = 1, 2 do
  print(i)
end
will run exactly twice. printing 1 and 2.

this is what would not run.

for i = 1, 2, -1 do
  print(i)
end

Yeah I failed as always….
ChunLing #9
Posted 08 February 2013 - 10:42 AM
but… i actually found it out.. when i used the for i = 1,2 -1 it actually worked.

(cur code)
What do you mean by "actually worked"? Do you mean that it skipped the loop, or that it ran the loop forever? Or something else? The syntax is valid, but the expected behavior would be for the loop to resolve on the first test because i is already less than 2 (and the loop is set to decrement).
Goof #10
Posted 08 February 2013 - 10:55 AM
I mean, now it did what it was supposed to do ;)/>
ChunLing #11
Posted 08 February 2013 - 11:38 AM
Well, then what it was supposed to do must have involved skipping that loop.
Goof #12
Posted 08 February 2013 - 11:52 AM
I didnt skip teh loop, i just added the -1 xD.

ehm could you help me in my other post: "t"api goto("chargecheck") not working.
i really want the işsue solved :)/>

thanks
ChunLing #13
Posted 08 February 2013 - 12:00 PM
Tell you what, put a line like:
print("Hah hah hah, ChunLing is a poopy-head!")
inside of that loop, and see if your program ever does that.

A for loop of the form:
for i = 1,2 -1 do ... end
should be functionally equivalent to
do local i = 1
  while i >= 2 do ...
	i = i-1
  end
end
Which is to say, it assigns the local i = 1, checks if it is less than 2, and hey, it is, so we skip to the end and keep going.
Edited on 08 February 2013 - 11:02 AM
KillaVanilla #14
Posted 08 February 2013 - 02:13 PM
Tell you what, put a line like:
print("Hah hah hah, ChunLing is a poopy-head!")
inside of that loop, and see if your program ever does that.

A for loop of the form:
for i = 1,2 -1 do ... end
should be functionally equivalent to
do local i = 1
  while i >= 2 do ...
	i = i-1
  end
end
Which is to say, it assigns the local i = 1, checks if it is less than 2, and hey, it is, so we skip to the end and keep going.
Yeah, they're functionally equivalent. That is to say, they don't run at all.
Djerun #15
Posted 09 February 2013 - 11:07 AM
for i = 1, 2 - 1 do ... end <=> for i = 1,1 do ... end
(runs once)
while
for i = 1, 42 - 1 do ... end <=> for i = 1, 41 do ... end
(runs 41 times)

EDIT:
for i = x,y do ... end
<=>

do local i = x
  while i <= y do
    ...
    i = i + 1
  end
end

in the first example x = 1 and y = (2 - 1)
ChunLing #16
Posted 09 February 2013 - 08:46 PM
How did I not spot that?

Yes, without a comma between 2 and -1, it will be evaluated as a single expression, rather than two separate parameters.

"Hah hah hah, ChunLing is a poopy-head!"