Help needed with Picaxe maths please

stevesmythe

Senior Member
In post #2 of this thread, Boriz uses what looks like some very compact code but I can't get my head round the Picaxe maths. Please could someone translate these two lines into conventional maths and explain it for me? I have tried reading the manual but I still can't understand it.

b1=b1+2 max 3//3 and

b0=b0+b1-1//4

I'd be much obliged. Thanks.
 

PieM

Senior Member
Picaxe Manual2 :
The division (/) command returns the quotient (whole number) word of a word*word division.
The modulus (// or %) command returns the remainder of the calculation.
 

stevesmythe

Senior Member
Yes, I have read the manual, but the left-right order of calculation plus the modulus combined with the MAX leaves me confused. Perhaps you could rewrite those two expressions in conventional maths for me?
 

eggdweather

Senior Member
The maths is processed left to right with no precedence.
The line is processed like this:
B1 is added to 2, then that result is limited to 3 then divided by 3 returning the modulus
If b1+2 was 3 then max has no effect and 3//3 would be 0 there was no remainder
If b1+2 was 10 then max 3 would yield 3 as above 3//3 would be 0
If b1+2 was 2 then max 3 would be 2 and 2//3 would be 2
 

stevesmythe

Senior Member
OK. Thanks eggdweather. Got it now.

I was also puzzled by b0=b0+b1-1//4 in the case where b0=0 and b1=0.

That would mean that b0=(0+0-1)//4 or -1 mod 4 but I thought that Picaxe can't handle negative numbers?
 

westaust55

Moderator
Correct, the PICAXE math does not "handle" negative numbers.

What happens if the result of a subtraction gives a value less than zero is that the result has under flow.

If you are using a byte variable for the result,
0 - 1 ==> 255
Think of a byte (0 - 255 range) and 0 equates to 256 i.e. The lower 8 bits (= byte) are all zero's.

For a word variable result and during internal math (always performed to 16-bits)
0 - 1 ==> 65535
 

hippy

Ex-Staff (retired)
It seems that "b1=b1+2 max 3//3" is intended to toggle b1 between 0 and 2 every time it is executed. Not sure why it's done that way but would guess "because it works".

An alternative would be -

b1 = b1 ^ 2
 

hippy

Ex-Staff (retired)
So, -1 mod 4 (=3) is evaluated as 65535 mod 4 which is also 3.
That is correct. It's basically creating running a repeating sequence 0 to 3 then switching to a reversed repeating sequence, 3 to 0.

The way I would have done it is ...

Code:
b1 = 1
Do
  For w2 = 0 To 20
    LookUp b0,("A","B","C","D"),b2
    SerTxd( b2 )
    b0 = b0 + b1 // 4
  Next
  b1 = -b1
Loop
Run that through PE6 simulator and you will see "ABCDABCDA..." then it switches to "DCBADCBAD...", then back to "ABCDA..." again.
 
Top