# Understanding max min

#### rq3

##### Senior Member
I don't think this does what I intend it to do (limit s_w3 to values between 0 and 995, without "rolling over):
s_w3=1023-s_w3 max 995 min 0

What should this look like?

Thanks,
Rip

#### inglewoodpete

##### Senior Member
You need to use Min before doing a calculation. Any integer in PICAXE basic will have a minimum value of 0. So 0 - 1 = 65535 (word mathematics) or 0 - 1 = 255 (byte mathematics).

I'm not sure what you're trying to do with your equation above, so it's difficult for me to provide a solution.

The best description I can give for Min is, if you don't want to roll the variable over, do your limiting first:

w0 = w0 Min 8 - 8
or
w0 = w0 Min w1 - w1
or
w0 = w0 Min b2 - b2

Similar rules apply to Max, at the other end of the scale. Put simply, Min and Max do not, in themselves, prevent an integer rollover in a variable.

#### marks

##### Senior Member
Hi rq3,
I haven't used max or min but looks like your also trying to invert to 995 to 0.
perhaps try

Rich (BB code):
``w3=w3 max 995 min 0 : W3 =995-w3``

#### hippy

##### Technical Support
Staff member
s_w3=1023-s_w3 max 995 min 0

What should this look like?
Possibly -

s_w3 = 1023 Min s_w3 - s_w3 Max 995

Though, as inglewoodpete, says that may not be what you want to achieve. If, as marks suspects, you simply want to turn 0-995 into 995-0, I'd use ...

s_w3 = 995 Min s_w3 - s_w3

#### inglewoodpete

##### Senior Member
Hi rq3,
I haven't used max or min but looks like your also trying to invert to 995 to 0.
perhaps try

Rich (BB code):
``w3=w3 max 995 min 0 : W3 =995-w3``
marks, As I demonstrated above, "min 0" is meaningless, since a PICAXE integer can never be less than 0.

#### BESQUEUT

##### Senior Member
For all basics in the world (Except Picaxes) :
min(2,3)
==> 2 tibasicdev.wikidot.com

Also, why is the MIN operator so difficult to find in the doc ?
And when you find it, the explanation is a nonsens :
limit value to a minimum value
What does that mean ?
Now, i know that this is trap, but think about learners...

#### marks

##### Senior Member
It is interesting the different ways you can use min and max, i found it easier starting off with the varible in front
Rich (BB code):
`````` w3 = w3 max 995             ;     0 to 995>65535 = 0 to 955

w3 = w3 min 14 max 1009-14  ;  0<14 to 1009>65535  = 0 to 995``````
and hippy's example a little trickier but you save a few bytes when wanting to invert the result
Rich (BB code):
`````` w3 = 1023 Min  w3 -  w3  Max 995      ; 0<28 to 1023>65535 = 995 to 0

w3 =  995 Min  w3 -  w3               ;    0 to 995>65535 = 995 to 0``````

#### hippy

##### Technical Support
Staff member
The main lesson to take away is that "X - N Min 0" doesn't work, doesn't prevent underflow wrapround.

That has to be "X Min N - N"

#### rq3

##### Senior Member
The main lesson to take away is that "X - N Min 0" doesn't work, doesn't prevent underflow wrapround.

That has to be "X Min N - N"
Thanks everyone! This simulates properly:

Code:
``````#picaxe20m2

main:
for w0=0 to 1023 step 5
w1=995 Min w0-w0
sertxd (#w0,"  ",#w1,cr,lf)
next w0``````
w0 is a readadc10 result, which is then used to modify the duty cycle of a pwmout command. The idea is to prevent rollover if the result is "below zero", and to limit the maximum duty cycle to 995.

#### Protezoid

##### New Member
Hi, how come the value is not limited to a minimum of 0, the min command is ignored and value is 255 with the code below:

b0=5
b1= 4 min 0 - b0

I tried variations, either 255 or syntax error
thanks

#### hippy

##### Technical Support
Staff member
how come the value is not limited to a minimum of 0
MIN means ensure greater than a minimum. 4 is bigger than the minimum 0 so it remains as 4. When you subtract 'b0', if that's greater than 4 the result would go negative, but negatives are large positive numbers, eg 5-4 = -1, -1 is \$FFFF (65535). When that is truncated to an 8-bit byte it becomes \$FF (255).

For your example you would probably need -

b1 = 4 min b0 - b0