Variable overflow

matchbox

Senior Member
When using + or - in variable mathematics. Will the MIN or MAX command prevent a less than zero overflow from occuring?

e.g. Code

do
if b1>100 then dec b1 endif
if b0<10 then let b1=b1-4 MIN 1 endif
if b1=<3 then goto motor_off
loop

The idea of this example code, is to increase the decrement rate of a variable counter( 4 times >); without it going below zero. i.e. stopping at ONE.
 

srnet

Senior Member
I suspect not, but I don't have access to the simulator to check.

Expressions are not evaluated as a whole but left to right, b1 could go to 255 say, and the MIN 1 wont have an effect ?
 

matchbox

Senior Member
I might not have explained it properly. When b1=b1-4... it is to speed up the decrement rate, by 4 times . What i didn't want to happen , is for b1 to go below zero.

i.e. if b1= 3 and then b1=b1-4. Then b1 would equal -1.

Would setting b1 MIN 1, stop this from happening?
 

matchbox

Senior Member
Sorry about that srnet. I didnt realise that it would go back to 255.

Allycat, would it have the same effect if it was written.... b1=b1MIN1-4? The reason is due to the fact that i might set the value to -20 to make it decrement very fast. I would like b1 to go down to 1, but not underflow.
 

hippy

Technical Support
Staff member
you should use b1 = b1 MIN 4 - 4 to prevent an underflow.
There are a few tricks to help make that easier, particularly where the number may change during development. First, simply defining the number as a SYMBOL constant ...

Code:
[color=Blue]Symbol N [/color][color=DarkCyan]= [/color][color=Navy]4[/color]

[color=Purple]b1 [/color][color=DarkCyan]= [/color][color=Purple]b1 [/color][color=DarkCyan]Min [/color][color=Blue]N [/color][color=DarkCyan]- [/color][color=Blue]N[/color]
Or using a #DEFINE or #MACRO ...

Code:
[color=Navy]#Define [/color][color=Black]DecToZero[/color][color=Blue]( [/color][color=Black]var, N [/color][color=Blue]) [/color][color=Black]var [/color][color=DarkCyan]= [/color][color=Black]var [/color][color=DarkCyan]Min [/color][color=Black]N [/color][color=DarkCyan]- [/color][color=Black]N

DecToZero[/color][color=Blue]( [/color][color=Purple]b1[/color][color=Black], [/color][color=Navy]4 [/color][color=Blue])[/color]
Code:
[color=Navy]#Macro [/color][color=Black]DecToZero[/color][color=Blue]( [/color][color=Black]var, N [/color][color=Blue])
  [/color][color=Black]var [/color][color=DarkCyan]= [/color][color=Black]var [/color][color=DarkCyan]Min [/color][color=Black]N [/color][color=DarkCyan]- [/color][color=Black]N
#EndMacro

DecToZero[/color][color=Blue]( [/color][color=Purple]b1[/color][color=Black], [/color][color=Navy]4 [/color][color=Blue])[/color]
And if you want to make it look more like a function ...

Code:
[color=Navy]#Define [/color][color=Black]SubMinZero[/color][color=Blue]( [/color][color=Black]var, N [/color][color=Blue]) [/color][color=Black]var [/color][color=DarkCyan]Min [/color][color=Black]N [/color][color=DarkCyan]- [/color][color=Black]N[/color]

[color=Purple]b1 [/color][color=DarkCyan]= [/color][color=Black]SubMinZero[/color][color=Blue]( [/color][color=Purple]b1[/color][color=Black], [/color][color=Navy]4 [/color][color=Blue])[/color]
The 4 can also be replaced by a SYMBOL constant if desired.
 
Last edited:

AllyCat

Senior Member
Hi,

would it have the same effect if it was written.... b1=b1MIN1-4?
No, you must use the format as I (and now hippy) show above.

In PICaxe Basic a byte cannot have a negative value, it must be between 0 and 255 (inclusive). The Programme Editor does "understand" negative values, and also values above 65535 (16 bits), to a limited extent, but the resulting bytes or words can only be interpreted in the program within the corresponding positive range. For example:

Code:
b1 = - 20
sertxd (#b1)
Do you actually want a minimum value of 0 or 1 ? An underflow occurs below zero, not 1. For a minimum of 1 you should use, for example b1 = b1 MIN 21 - 20 .

Cheers, Alan.
 

hippy

Technical Support
Staff member
Allycat, would it have the same effect if it was written.... b1=b1MIN1-4?
No. b1 would be set to a minimum of 1 then, if b1 has a value of 1, 2 or 3, subtracting 4 would underflow. You need to set it to a minimum of the value being subtracted.

The reason is due to the fact that i might set the value to -20 to make it decrement very fast. I would like b1 to go down to 1, but not underflow.
This is one way you can do that, decrementing by 20 but not allowed to go below 1 ...

Code:
[color=Navy]#Macro [/color][color=Black]DecToMinimum[/color][color=Blue]( [/color][color=Black]var, N, minimum [/color][color=Blue])
  [/color][color=Black]var [/color][color=DarkCyan]= [/color][color=Black]minimum [/color][color=DarkCyan]+ [/color][color=Black]N [/color][color=DarkCyan]Min [/color][color=Black]var [/color][color=DarkCyan]- [/color][color=Black]N[/color]
[color=Navy]#EndMacro[/color]

[color=Black]DecToMinimum[/color][color=Blue]( [/color][color=Purple]b1[/color][color=Black], [/color][color=Navy]20[/color][color=Black], [/color][color=Navy]1 [/color][color=Blue])[/color]
That works for byte variables, and mostly for word variables but won't work as expected if 'minimum+N' overflows.

The MIN operator is commutative so "minimum + N Min var" is the PICAXE equivalent of the mathematical "var Min (minimum + N)".
 
Top