min command

RobertN

Member
Am trying to use the min command in 08M programming editor, it ignors the min command in this code.

let w3 = w2 - w1 min 0

When w1 is greater than w2 the result rolls over to 65--- without limiting the result to 0.

When min 0 is entered into the editer it appears in black, not blue like shown in the basic commands manual 2, page 20.

Shouldn't min show up in blue? If so how do you change it. Or what can be done to limitthe value to 0?
 

lbenson

Senior Member
Picaxe math is positive integers only--min 0 can never be applicable because a number cannot be less than zero. To do what you want, try

let w3 = w2 - w1
if w1 < w2 then : w3 = 0 : endif
 

RobertN

Member
The problem is, w1 can be a lower value than w2, ie. let w3 = w2(30) - w1(20). The result (10) is a valid value.

Any w3 value down to zero (or 1) is required for the rest of the program.

Normally either w1 or w2 can be the larger value.

When w1 is a larger
than w2, then w3 rolls over to 65---. which can be expected.

Any clue why min is not working? I'd like to use it in othe parts of the program.
 

westaust55

Moderator
Please note that the PICAXE math functions MIN and MAX are not operators in the usual sense as used in say MS Excel. IN excel it fins the minimun value from a group;/list of values.

With PICAXE BASIC, the MIN defines the minimum value that is allowed,

So if you have the statement:

let w1 = 100 - w0 min 20 ; note using 20 not 0 here

as w0 increases from 0 to 80 the vlaue of 100- w0 is >= 20
but when w0 is greater than 80, then the part 100 - w0 is less than 20.
In this case the MIN fucntions comes into play and effectively says the minimum result is 20.

thus w1 = 100 - 99 MIN 20 will give w1 = 20

So having a MIN fucntion with a value of 0 is pointless.

Have a good read at these MIN and MAX fuinciton descriptions in PICAXE manual 2 page 21.
 

westaust55

Moderator
The problem is, w1 can be a lower value than w2, ie. let w3 = w2(30) - w1(20). The result (10) is a valid value.

Any w3 value down to zero (or 1) is required for the rest of the program.

Normally either w1 or w2 can be the larger value.

When w1 is a larger
than w2, then w3 rolls over to 65---. which can be expected.

Any clue why min is not working? I'd like to use it in othe parts of the program.
AH so try:

Code:
IF W2 > w1 THEN
  w3 = w2 - w1
ELSE
  w3 = w1 - w2
ENDIF


If you have math fuctions where the result might normally go negative, the PICAXE does not handle negative values and instead rolls over, so -1 become 255.  Think of negative PICAXE results as:
 256 - the negative value for byte variables
and as
65536 - the negative value for word variables
 
Last edited:

Jeremy Leach

Senior Member
What I've done in this situation is temporarily raise the level so there isn't any rollover. But this only works if you've got enough 'headroom' ...

For example:

Code:
W3 = 5000 + W2 - W1 MIN 5000 - 5000
 
Last edited:

hippy

Ex-Staff (retired)
let w3 = w2 - w1 min 0

As mentioned above, PICAXE variables can only hold zero and positive values and that includes internal intermediate calculations so all results are zero or positive, always at or above a minimum of zero so "min 0" has no effect.

This should get what you want ...

let w3 = w2 min w1 - w1

When w2 is greater than w1 the result in w3 is (w2-w1), when w2 is less than or equal to w1 the result in w3 will be zero.
 

RobertN

Member
As a result of this min thing, I've revised the code. And have another line of code to prevent crossing over 0, as w1 is incremented up and down. w2 and w3 ane gone as is the subtraction.

let w1 = 20

if pin1 = 0 inc w1 endif

if pin2 = 0 dec w1 endif

if w1 = 0 then inc w1 endif

This only works since w1 can only decremented by 1 each loop through the code. A result of 1 is ok.

Trialed hippy's fix, and it works. Not sure how to incorporate it in the code above.
 

inglewoodpete

Senior Member
As hippy implies, the MIN function must occur BEFORE you do your subtraction. It must be used to ensure the number you are subtracting from is big enough to 'withstand' the subtraction.

In the case above:
Code:
if pin2 = 0 Then
   dec w1
endif

should be replaced with .....

if pin2 = 0 Then
   w1 = w1 Min 1 - 1   'Ensure w1 never 'ticks over' to 65535
endif
This is an area that could be improved in the manual: most people get caught on MIN the first time they try use use it. Even little old me :)
 

RobertN

Member
Thanks guys.

The examples in the manual show the 'min' funtion after the math. Is there a difference in how 'min' is used between multiplying and dividiing per the manual examples and adding and subtracting?
 

hippy

Ex-Staff (retired)
Not quite sure how you mean. 'Min' does its thing at the point where it is in the expression, regardless of what other operations precede or follow it.
 
Top