( and ) in mathematical expressions

pjrebordao

Senior Member
Using a 20X2, the following expression gives me a syntax error:

let Z = Z * (Y2 - Y1)

where Z, Y2, Y1 are word variables.

Shouldn'it work in a 20X2 ?
 

BeanieBots

Moderator
marks, that gives a different result...

So, am I the only one trying to use brackets ?
The code posted by marks should work.
Please post an example set of values of actual result and expected result.

As for brackets, promised a while back but we're still waiting.
Meanwhile, just re-arrange as described above. (It does work).
 

bluejets

Senior Member
The code posted by marks should work.
Please post an example set of values of actual result and expected result.

As for brackets, promised a while back but we're still waiting.
Meanwhile, just re-arrange as described above. (It does work).
Does it work because picaxe program processes top to bottom, left to right..???
 

westaust55

Moderator
That is correct.
Calculations are performed left to right.

From PICAXE Manual2 page 23:
Maths is performed strictly from left to right. Unlike some computers and
calculators, the PICAXE does not give * and / priority over + and -.
 
Last edited:

g6ejd

Senior Member
If Z=1, Y2= 3 and Y1 = 2 then Z = 1 * (3 - 2) = 1 OR in PICAXE Z = 3 - 2 * 1 = 1 Correct
If Z=10, Y2= 1000 and Y1 = 100 then Z = 10 * (1000 - 100) = 9000 OR in PICAXE Z = 1000 - 100 * 10 = 9000 Correct
If Z=100, Y2= 10000 and Y1 = 100 then Z = 100 * (10000 - 100) = 990000 OR in PICAXE Z = 10000 - 100 * 100 = 6960 Incorrect!
 

Buzby

Senior Member
If Z=1, Y2= 3 and Y1 = 2 then Z = 1 * (3 - 2) = 1 OR in PICAXE Z = 3 - 2 * 1 = 1 Correct
If Z=10, Y2= 1000 and Y1 = 100 then Z = 10 * (1000 - 100) = 9000 OR in PICAXE Z = 1000 - 100 * 10 = 9000 Correct
If Z=100, Y2= 10000 and Y1 = 100 then Z = 100 * (10000 - 100) = 990000 OR in PICAXE Z = 10000 - 100 * 100 = 6960 Incorrect!
The last line is not incorrect, it's exactly what you would expect from a 16 bit integer maths engine.

A 'word' variable can store a value between 0 and 65535, see page 11 of Manual 2.
 

srnet

Senior Member
If y1>y2 may result in overflow error
Can result in an overflow yes.

Whilst overflows may not be what we want to see, they are not errors as far as the PICAXE is concerned, and you don't know if the overflow has occurred unless you explicitly test for it.
 

g6ejd

Senior Member
The last line is not incorrect, it's exactly what you would expect from a 16 bit integer maths engine.

A 'word' variable can store a value between 0 and 65535, see page 11 of Manual 2.
For the OP that's what I meant (should have been clearer for other readers), yes the result is correct, but not in mathematical terms, I was illustrating the point of 16-bit word maths.
 
Top