Page 1 of 1

Tricky Rotation problem

Posted: Mon Sep 10, 2007 1:47 pm
by PeterGams
New to PB, Hard to explain please bare with me.
Not sure if this is a bug or not.
Last col. rotated by 2 each time.
2nd last col. rotated by 2 every 4th count.
3rd last col rotated by 2 every 16th count.
4th last col. rotated by 2 every 64th count.
Numbers are not to use the very first line of numbers in the debug screen.
at line 257. 3rd number is using the very first number in the debug screen .
only once then continues where it should.
A bug or just bad code?

Code: Select all

Dim b.l(6) 
b(1) = 3 : b(2) = 4 : b(3) = 1 :b(4) = 6 : b(5) = 7 : b(6) = 2
Debug "Line :- "+Str(b(1))+" "+Str(b(2))+" "+Str(b(3))+" "+Str(b(4))+" "+Str(b(5))+" "+Str(b(6))
Debug "-----------------"
b(1)+2:b(2)+2:b(3)+2:b(4)+2:b(5)+2:b(6)+2
For i.l = 1 To 260
   ; 
   Debug Str(i)+"  :-    "+Str(b(1))+" "+Str(b(2))+" "+Str(b(3)) +" "+Str(b(4))+" "+Str(b(5))+" "+Str(b(6))
 

;-----Chamber 6 rotate 2 OK------ 
   If b(6)  = 0 : b(6)+ 2: EndIf
   b(6) = (b(6)+ 2) % 10
;--------------------------------

;-----Chamber 5 rotate 4 OK------ 
  If i %4 =0  : b(5) + 2 %10 :EndIf
  If b(5) = 7 : b(5) + 2 % 10:EndIf 
   b(5) = b(5) % 10
;--------------------------------
   
;-----Chamber 4 Rotate 16 OK-----  
  If i %16 =0 : b(4) + 2 %10 :EndIf
  If b(4) = 6 : b(4) + 2 % 10:EndIf
   b(4) = b(4) % 10
;--------------------------------


;-----Chamber 3 Rotate 64 ** ROTATION NOT WORKING
  If i %64 =0 : b(3) + 2 %10 :EndIf
  If b(3) = 1 : b(3) + 2 % 10 :EndIf
  b(3) = b(3) % 10
;------------------------



Next i

Posted: Mon Sep 10, 2007 3:54 pm
by Kaeru Gaman
at first glance:

Code: Select all

a = 5
b = 5

For n=0 To 9
  a + 2 % 10
  b = (b+2)%10
  Debug Str(a) + " <-> " + Str(b)
Next
at second glance:
you have two following Ifs.
1st: you count up when a certain i-counter value is reached (%4, %8)
2nd: you additionally count up when a certain digit value appears.
...may be biting itself...

Posted: Mon Sep 10, 2007 5:59 pm
by Demivec
PeterGams wrote:A bug or just bad code?
The modulo operator % has priority over addition. Which would result in your code being the equivalent of the below.

Code: Select all

Dim b.l(6)
b(1) = 3 : b(2) = 4 : b(3) = 1 :b(4) = 6 : b(5) = 7 : b(6) = 2
Debug "Line :- "+Str(b(1))+" "+Str(b(2))+" "+Str(b(3))+" "+Str(b(4))+" "+Str(b(5))+" "+Str(b(6))
Debug "-----------------"
b(1)+2:b(2)+2:b(3)+2:b(4)+2:b(5)+2:b(6)+2
For I.l = 1 To 260
  ;
  Debug Str(I)+"  :-    "+Str(b(1))+" "+Str(b(2))+" "+Str(b(3)) +" "+Str(b(4))+" "+Str(b(5))+" "+Str(b(6))
 
  ;-----Chamber 6 rotate 2 OK------
  If b(6)  = 0 : b(6)+ 2: EndIf
  b(6) = (b(6)+ 2) % 10
  ;--------------------------------
  
  ;-----Chamber 5 rotate 4 OK------
  If I %4 =0  : b(5) + 2 :EndIf
  If b(5) = 7 : b(5) + 2:EndIf
  b(5) = b(5) % 10
  ;--------------------------------
  
  ;-----Chamber 4 Rotate 16 OK----- 
  If I %16 =0 : b(4) + 2 :EndIf
  If b(4) = 6 : b(4) + 2:EndIf
  b(4) = b(4) % 10
  ;--------------------------------
  
  ;-----Chamber 3 Rotate 64 ** ROTATION NOT WORKING
  If I %64 =0 : b(3) + 2 :EndIf
  If b(3) = 1 : b(3) + 2:EndIf
  b(3) = b(3) % 10
  ;------------------------

Next I
Can you see the error? You are adding 2 % 10 which equals 2. In most of the rotations you suceed, by luck, because the number you are checking for is 3 or more below the max (2,4,16, etc.) or is even. For the 64 rotation you add 2 to 9 which equals 11. You check if it equals 1, it doesn't, then you take the modulo 10 and get 1. The next time through the loop it finds this value of 1 during the check and changes it.

You should corect your code as follows:

Code: Select all

;-----Chamber 3 Rotate 64 ** ROTATION NOT WORKING
  ;If I %64 =0 : b(3) + 2 %10 :EndIf
  ;You should use the form b(3)=(b(3) + 2) % 10 on each line to ensure
  ;the desired results.  But a pair of parens with fix your immediate problem.
  If I %64 =0 : b(3)=(b(3) + 2) %10 :EndIf
  If b(3) = 1 : b(3) + 2 % 10 :EndIf 
  b(3) = b(3) % 10
;------------------------
Technically these are cycles or sequences, not rotations.

Hey Demivec

Posted: Tue Sep 11, 2007 11:18 am
by PeterGams
Thanks heaps mate.
I tested your changes and they work spot on.

I'm just trying to work out why but for now
Thanks heaps Mate..